Ketan Patil
Ketan Patil

Reputation: 3

Simple Neural Network gives NaN loss

So I am practicing Deep Learning, This is one of my very first practice projects, I took the dataset from kaggle, containing 2 folders, one having images of people with mask and other without mask. I tried to build a simple NN but while fitting the data it gives loss:NaN, accuracy 0.4903 for all epochs from the start, can someone help me find out which part of the code is making error

here is the code

#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pandas as pd
import numpy as np
import sklearn.model_selection as train_test_split
import sklearn
import matplotlib.pyplot as plt
import tensorflow as tf
import glob
import tensorflow.keras.layers as layers
import cv2


# In[2]:


# Mapping Mask images

filenames_mask = [i for i in glob.glob('with_mask\*.jpg')]
filenames_mask = np.array(filenames_mask)
filenames_mask = pd.DataFrame(filenames_mask,columns=['image_path'])
filenames_mask.head()
is_mask = np.ones(filenames_mask.shape[0])
filenames_nomask = [i for i in glob.glob('without_mask\*.jpg')]
filenames_nomask = np.array(filenames_nomask)
filenames_nomask = pd.DataFrame(filenames_nomask,columns=['image_path'])
filenames_nomask.head()
not_mask = np.zeros(filenames_nomask.shape[0])

data = pd.concat([filenames_mask,filenames_nomask],ignore_index=True)
is_mask = pd.DataFrame(is_mask,columns=['is_mask'])
no_mask = pd.DataFrame(not_mask,columns=['is_mask'])
mask = pd.concat([is_mask,no_mask],ignore_index=True)
mask=mask.astype(int)
Data = data.join(mask)
Data = Data.sample(frac=1)


# In[3]:


# Mapping Images to X
X=[]
for i in Data['image_path']:
    img = cv2.imread(i)
    img = cv2.resize(img,(32,32))
#     img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#     img = img/255
    X.append(img)
X = np.array(X)
X.shape


# In[4]:


# Mapping Image Prediction to Y
Y = Data['is_mask']


# In[5]:


X_train,X_test,Y_train,Y_test = sklearn.model_selection.train_test_split(X,Y,random_state=2)


# In[ ]:





# In[6]:





# In[7]:





# In[23]:


# Building Simple Neural Network

SNN_Model = tf.keras.Sequential([
#     layers.Conv2D(38,(3,3),activation='relu',input_shape=(32,32,3)),
#     layers.MaxPooling2D(2,2),
#     layers.Conv2D(64,(3,3),activation='relu'),
#     layers.MaxPooling2D(2,2),
#     layers.Conv2D(64,(3,3),activation='relu'),
    
    layers.Flatten(),
    layers.Dense(1024,activation='relu'),
    layers.Dense(512,activation='relu'),
    layers.Dense(126,activation='relu'),
    layers.Dense(1,activation='softmax'),
])


# In[24]:


SNN_Model.compile(optimizer='sgd',loss='CategoricalCrossentropy',metrics=['accuracy'])


# In[25]:


SNN_Model.fit(X_train,Y_train,epochs=5)


# In[ ]:

Upvotes: 0

Views: 124

Answers (1)

Adarsh Wase
Adarsh Wase

Reputation: 1890

Maybe the error is because you are using softmax function instead of sigmoid, try this code:

SNN_Model = tf.keras.Sequential([
    layers.Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3)),
    layers.MaxPooling2D(2,2),
    layers.Conv2D(64,(3,3),activation='relu'),
    layers.MaxPooling2D(2,2),

    layers.Flatten(),
    layers.Dense(1024, activation='relu'),
    layers.Dense(512, activation='relu'),
    layers.Dense(128, activation='relu'),
    layers.Dense(1, activation='sigmoid'),
])

And binary_crossentropy will work more efficient in this case.

SNN_Model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Upvotes: 1

Related Questions