I am training a medical dataset and the images are 3D arrays and I added a channel layer to run 3D CNN neural net. The images are only 25 and when I run the neural network the accuracy and loss do not change with epochs at all. after every epoch the accuracy is constant. I believe after every epochs the weights should change and accuracy should atleast increase by one percent but nothing happens here. I don't have a clue what is going wrong here.
#X shape=[25,100,100,100]
#input_shape_=[100,100,100]
tf.keras.backend.set_image_data_format('channels_last')
input_shape_=[resize_num,resize_num,resize_num,1]
model = tf.keras.models.Sequential([
tf.keras.layers.Conv3D(12, (2,2,2), activation='relu',input_shape=input_shape_),
tf.keras.layers.MaxPooling3D((2,2,2)),
tf.keras.layers.Conv3D(18, (2,2,2), activation='relu'),
tf.keras.layers.MaxPooling3D((3,3,3)),
tf.keras.layers.Conv3D(36, (3,3,3), activation='relu'),
tf.keras.layers.MaxPooling3D((3,3,3)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(200,'relu'),
tf.keras.layers.Dense(120,'relu'),
tf.keras.layers.Dense(60,'relu'),
tf.keras.layers.Dense(10,'relu'),
tf.keras.layers.Dense(1,'relu')
])
#model.summary()
model.compile(optimizer='Adam',loss='BinaryCrossentropy',metrics=['accuracy'])
model.fit(X_train,y,epochs=10,batch_size=1)
Epoch 1/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 2/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 3/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 4/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 5/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 6/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 7/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 8/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 9/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
Epoch 10/10
25/25 [==============================] - 32s 1s/step - loss: 5.5460 - accuracy: 0.6400
<keras.callbacks.History at 0x2ba76127cc8>
The last layer activation function is not proper for this problem as binary classification model, change it into sigmoid activation function
...
tf.keras.layers.Dense(1,'sigmoid')
...
Related
Here is my complete code:
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Input
from tensorflow.keras.models import Model
import numpy as np
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt
train_cats_path = r"C:\Users\jrand\Downloads\dogscats\train\cats"
train_cats = [train_cats_path+"\\"+f for f in listdir(train_cats_path) if isfile(join(train_cats_path, f))]
train_cat_array = []
for tmp_cat in train_cats:
img = load_img(tmp_cat, target_size=(224, 224)) # Load image in with desired dimensions
img = img_to_array(img) # Convert the image to a numpy array
img = img.reshape(224, 224, 3)
img = preprocess_input(img)
train_cat_array.append(img)
x_train_cat_labels = np.array([1.0]*len(train_cat_array))
x_train_cat_images = np.array(train_cat_array)
train_dogs_path = r"C:\Users\jrand\Downloads\dogscats\train\cats"
train_dogs = [train_dogs_path+"\\"+f for f in listdir(train_dogs_path) if isfile(join(train_dogs_path, f))]
train_dog_array = []
for tmp_dog in train_dogs:
img = load_img(tmp_dog, target_size=(224, 224)) # Load image in with desired dimensions
img = img_to_array(img) # Convert the image to a numpy array
img = img.reshape(224, 224, 3)
img = preprocess_input(img)
train_dog_array.append(img)
x_train_dog_labels = np.array([0.0]*len(train_dog_array))
x_train_dog_images = np.array(train_dog_array)
print("len of dog images", len(x_train_dog_images), "len of cat images", len(x_train_cat_images))
print("len of dog labels", len(x_train_dog_labels), "len of cat labels", len(x_train_cat_labels))
x_train_images = np.concatenate([x_train_dog_images, x_train_cat_images])[0:1000]
x_train_labels = np.concatenate([x_train_dog_labels, x_train_cat_labels])[0:1000]
model = VGG16(weights = 'imagenet', include_top = False, input_shape = (224, 224, 3))
for layer in model.layers:
layer.trainable = False
x = Flatten()(model.output)
predictions = Dense(1, activation="sigmoid")(x)
new_model = Model(model.input, predictions)
# compile model
model.compile(optimizer="Adam", loss="binary_crossentropy", metrics=["accuracy"])
# train model
history = model.fit(x_train_images, x_train_labels, batch_size=1, epochs=100)
So, as you can see, I take the images of cats and dogs, generate labels for them, then I concatenate the two arrays together using numpy so that I can then train on them.
I guess my problems are as follows:
I have to reduce the size of my training set by splicing the arrays to the first 1000 images, which doesn't help things
I have to use a batch_size of 1, also doesn't help things
Can anyone give me some tips for improving this code and the NN performance?
Here is sample output as things currently stand:
len of dog images 11500 len of cat images 11500
len of dog labels 11500 len of cat labels 11500
2022-03-04 11:46:54.410085: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-03-04 11:46:55.170021: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 6613 MB memory: -> device: 0, name: NVIDIA GeForce GTX 1080, pci bus id: 0000:02:00.0, compute capability: 6.1
2022-03-04 11:46:55.170948: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 6613 MB memory: -> device: 1, name: NVIDIA GeForce GTX 1080, pci bus id: 0000:04:00.0, compute capability: 6.1
2022-03-04 11:46:56.198632: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/100
2022-03-04 11:46:56.725619: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8201
1000/1000 [==============================] - 9s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 2/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 3/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 4/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 5/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 6/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 7/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 8/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 9/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 10/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 11/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 12/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 13/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 14/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 15/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 16/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 17/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 18/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 19/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 20/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 21/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 22/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 23/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 24/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 25/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 26/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 27/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 28/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 29/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 30/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 31/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 32/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 33/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 34/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 35/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 36/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 37/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 38/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 39/100
1000/1000 [==============================] - 7s 7ms/step - loss: 2.1102 - accuracy: 1.0204e-04
Epoch 40/100
The reason I wrote this program is to get some experience with transfer learning. There may be better ways to accomplish this but this is the way I chose.
I updated the code and removed a loop I was messing with. Sorry about that.
I don't think the issue is the small dataset, since transfer learning is used to deal with smaller datasets.
The issue is that you are freezing all the layers of the pre-trained model (VGG), without adding any new Dense Layer. Then you call model.fit, but none of the layers are trainable. Therefore, nothing is allowed to change. In fact, your problem is not that you are getting very low accuracy, but that the accuracy doesn't change at all among epochs. This should be a red flag meaning something in your code is broken!
Try to add at least another Dense layer before the last.
EDIT:
You are also compiling and calling fit() on model instead of new_model.
I hope I've been helpful
I am training a convolutional model in keras.
The size of my training data is
(60000, 28, 28, 1)
I build a simple model here
model = Sequential()
model.add(
Conv2D(
filters=32,
kernel_size=(4, 4),
input_shape=(28, 28, 1),
activation='relu'
)
)
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
When I tried to fit this model to my data in the following line
model.fit(
x_train,
y_train_hot_encode,
epochs=20,
validation_data=(x_test, y_test_hot_encode),
)
I noticed something weird in the logs
Epoch 1/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.5311 - accuracy: 0.8109 - val_loss: 0.3381 - val_accuracy: 0.8780
Epoch 2/20
1875/1875 [==============================] - 19s 10ms/step - loss: 0.2858 - accuracy: 0.8948 - val_loss: 0.2820 - val_accuracy: 0.8973
Epoch 3/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.2345 - accuracy: 0.9150 - val_loss: 0.2732 - val_accuracy: 0.9001
Epoch 4/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.2016 - accuracy: 0.9247 - val_loss: 0.2549 - val_accuracy: 0.9077
Epoch 5/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1644 - accuracy: 0.9393 - val_loss: 0.2570 - val_accuracy: 0.9077
Epoch 6/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1434 - accuracy: 0.9466 - val_loss: 0.2652 - val_accuracy: 0.9119
Epoch 7/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1225 - accuracy: 0.9553 - val_loss: 0.2638 - val_accuracy: 0.9135
As you can see, each epoch was trained on 1875 images and not on the entire 60K images, why is that? or am I reading the log the wrong way?
It is because the number shown there is number of steps instead of number of examples trained. As you didn't supply batch_size into model.fit(), it used the default batch size 32.
The expected number of steps per epoch is ceil(60000/32) = 1875, consistent with what is shown in the log.
I'm new to tensorflow 2.0 and I'm running a very simple model that classifies a 1d time series of fixed size (100 values) into one of two classes:
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(512, activation='relu', input_shape=(100, 1)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
I have a dataset of ~660,000 labeled examples that I feed into the model with batch_size=256. When I train the NN for 10 epochs, using the same data as a validation dataset
history = model.fit(training_dataset,
epochs=10,
verbose=1,
validation_data=training_dataset)
I got the following output
Epoch 1/10
2573/2573 [==============================] - 55s 21ms/step - loss: 0.5271 - acc: 0.7433 - val_loss: 3.4160 - val_acc: 0.4282
Epoch 2/10
2573/2573 [==============================] - 55s 21ms/step - loss: 0.5673 - acc: 0.7318 - val_loss: 3.3634 - val_acc: 0.4282
Epoch 3/10
2573/2573 [==============================] - 55s 21ms/step - loss: 0.5628 - acc: 0.7348 - val_loss: 2.6422 - val_acc: 0.4282
Epoch 4/10
2573/2573 [==============================] - 57s 22ms/step - loss: 0.5589 - acc: 0.7314 - val_loss: 2.6799 - val_acc: 0.4282
Epoch 5/10
2573/2573 [==============================] - 56s 22ms/step - loss: 0.5683 - acc: 0.7278 - val_loss: 2.3266 - val_acc: 0.4282
Epoch 6/10
2573/2573 [==============================] - 55s 21ms/step - loss: 0.5644 - acc: 0.7276 - val_loss: 2.3177 - val_acc: 0.4282
Epoch 7/10
2573/2573 [==============================] - 56s 22ms/step - loss: 0.5664 - acc: 0.7255 - val_loss: 2.3848 - val_acc: 0.4282
Epoch 8/10
2573/2573 [==============================] - 55s 21ms/step - loss: 0.5711 - acc: 0.7237 - val_loss: 2.2369 - val_acc: 0.4282
Epoch 9/10
2573/2573 [==============================] - 55s 22ms/step - loss: 0.5739 - acc: 0.7189 - val_loss: 2.6969 - val_acc: 0.4282
Epoch 10/10
2573/2573 [==============================] - 219s 85ms/step - loss: 0.5778 - acc: 0.7213 - val_loss: 2.5662 - val_acc: 0.4282
How come the accuracy during the training is so different from the validation step, when run on the same dataset? I tried to find some explanation but it seems that such problems usually arise when people use BatchNormalization or Dropout layers, which is not the case here.
Based on the information above, I may assume your data has strong dependencies on examples that are closer to each other in time series.
Therefore, NN data flow will likely be like this:
NN takes the first batch, calculates the loss, and updates the weights and biases
the cycle repeats on and on
but since examples in batches not that far away from each other in time series it is easier for NN to update weights accordingly, making loss reasonably low for every next batch
When it is time for validation NN just calculates the loss without updating the weights,
so you end up with NN that learned how to infer on a small portion of the data but do not generalize well on the whole dataset.
That is why validation error is different from training even on the same dataset.
And a list of reasons is not limited to this, this is just one assumption.
I just started to work with tensorflow 2.0 and followed the simple example from its official website.
import tensorflow as tf
import tensorflow.keras.layers as layers
mnist = tf.keras.datasets.mnist
(t_x, t_y), (v_x, v_y) = mnist.load_data()
model = tf.keras.Sequential()
model.add(layers.Flatten())
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dropout(0.2))
model.add(layers.Dense(10))
lossFunc = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam', loss=lossFunc,
metrics=['accuracy'])
model.fit(t_x, t_y, epochs=5)
The output for the above code is:
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 4s 60us/sample - loss: 2.5368 - accuracy: 0.7455
Epoch 2/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.5846 - accuracy: 0.8446
Epoch 3/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.4751 - accuracy: 0.8757
Epoch 4/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.4112 - accuracy: 0.8915
Epoch 5/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.3732 - accuracy: 0.9018
However, if I change the lossFunc to the following:
def myfunc(y_true, y_pred):
return lossFunc(y_true, y_pred)
which just simply wrap the previous function, it performs totally differently. The output is:
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 4s 60us/sample - loss: 2.4444 - accuracy: 0.0889
Epoch 2/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.5696 - accuracy: 0.0933
Epoch 3/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.4493 - accuracy: 0.0947
Epoch 4/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.4046 - accuracy: 0.0947
Epoch 5/5
60000/60000 [==============================] - 3s 51us/sample - loss: 0.3805 - accuracy: 0.0943
The loss values are very similar but the accuracy values are totally different. Anyone knows what is the magic in it, and what is the correct way to write your own loss function?
When you use built-in loss function, you can use 'accuracy' as metric . Under the hood, tensorflow will select appropriate accuracy function (in your case it is tf.keras.metrics.SparseCategoricalAccuracy()).
When you define custom_loss function, then tensorflow doesn't know which accuracy function to use. In this case, you need to explicitly specify that it is tf.keras.metrics.SparseCategoricalAccuracy(). Please check the gist hub gist here.
The code modification and the output is as follows
model2 = tf.keras.Sequential()
model2.add(layers.Flatten())
model2.add(layers.Dense(128, activation="relu"))
model2.add(layers.Dropout(0.2))
model2.add(layers.Dense(10))
lossFunc = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model2.compile(optimizer='adam', loss=myfunc,
metrics=['accuracy',tf.keras.metrics.SparseCategoricalAccuracy()])
model2.fit(t_x, t_y, epochs=5)
output
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 5s 81us/sample - loss: 2.2295 - accuracy: 0.0917 - sparse_categorical_accuracy: 0.7483
Epoch 2/5
60000/60000 [==============================] - 5s 76us/sample - loss: 0.5827 - accuracy: 0.0922 - sparse_categorical_accuracy: 0.8450
Epoch 3/5
60000/60000 [==============================] - 5s 76us/sample - loss: 0.4602 - accuracy: 0.0933 - sparse_categorical_accuracy: 0.8760
Epoch 4/5
60000/60000 [==============================] - 5s 76us/sample - loss: 0.4197 - accuracy: 0.0946 - sparse_categorical_accuracy: 0.8910
Epoch 5/5
60000/60000 [==============================] - 5s 76us/sample - loss: 0.3965 - accuracy: 0.0937 - sparse_categorical_accuracy: 0.8979
<tensorflow.python.keras.callbacks.History at 0x7f5095286780>
Hope this helps
Just for the sake of the argument I am using the same data during training for training and validation, like this:
model.fit_generator(
generator=train_generator,
epochs=EPOCHS,
steps_per_epoch=train_generator.n // BATCH_SIZE,
validation_data=train_generator,
validation_steps=train_generator.n // BATCH_SIZE
)
So I would expect that the loss and the accuracy of training and validation at the end of each epoch would be pretty much the same? Still it looks like this:
Epoch 1/150
26/26 [==============================] - 55s 2s/step - loss: 1.5520 - acc: 0.3171 - val_loss: 1.6646 - val_acc: 0.2796
Epoch 2/150
26/26 [==============================] - 46s 2s/step - loss: 1.2924 - acc: 0.4996 - val_loss: 1.5895 - val_acc: 0.3508
Epoch 3/150
26/26 [==============================] - 46s 2s/step - loss: 1.1624 - acc: 0.5873 - val_loss: 1.6197 - val_acc: 0.3262
Epoch 4/150
26/26 [==============================] - 46s 2s/step - loss: 1.0601 - acc: 0.6265 - val_loss: 1.9420 - val_acc: 0.3150
Epoch 5/150
26/26 [==============================] - 46s 2s/step - loss: 0.9790 - acc: 0.6640 - val_loss: 1.9667 - val_acc: 0.2823
Epoch 6/150
26/26 [==============================] - 46s 2s/step - loss: 0.9191 - acc: 0.6951 - val_loss: 1.8594 - val_acc: 0.3342
Epoch 7/150
26/26 [==============================] - 46s 2s/step - loss: 0.8811 - acc: 0.7087 - val_loss: 2.3223 - val_acc: 0.2869
Epoch 8/150
26/26 [==============================] - 46s 2s/step - loss: 0.8148 - acc: 0.7379 - val_loss: 1.9683 - val_acc: 0.3358
Epoch 9/150
26/26 [==============================] - 46s 2s/step - loss: 0.8068 - acc: 0.7307 - val_loss: 2.1053 - val_acc: 0.3312
Why does especially the accuracy differ so much although its from the same data source? Is there something about the way how this is calculated that I am missing?
The generator is created like this:
train_images = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255
)
train_generator = train_images.flow_from_directory(
directory="data/superheros/images/train",
target_size=(299, 299),
batch_size=BATCH_SIZE,
shuffle=True
)
Yes, it shuffles the images, but as it iterates over all images also for validation, shouldn't the accuracy at least be close?
So the model looks like this:
inceptionV3 = keras.applications.inception_v3.InceptionV3(include_top=False)
features = inceptionV3.output
net = keras.layers.GlobalAveragePooling2D()(features)
predictions = keras.layers.Dense(units=2, activation="softmax")(net)
for layer in inceptionV3.layers:
layer.trainable = False
model = keras.Model(inputs=inceptionV3.input, outputs=predictions)
optimizer = keras.optimizers.RMSprop()
model.compile(
optimizer=optimizer,
loss="categorical_crossentropy",
metrics=['accuracy']
)
So no dropout or anything, just the inceptionv3 with a softmax layer on top. I would expect that the accuracy differs a bit, but not in this magnitude.
Are you sure your train_generator returns the same data when Keras retrieves training data and validation data, if it's a generator?
The name being generator, I'd expect it not to :)