'training and test using same data but test accuracy very low
I am new to machine learning, I have built a mobilenet model to classify gray image. the training accuracy was good(above 90%) but the test accuracy was very low(20%~30%).enter image description here enter link description here first I thought it's overfitting, I tried data augmentation, simplify model(take away most of it's convolution layer)
# A generic MobileNet block
def mobile_net_block(x, filters, stride, depth_multiplier, block_id):
prefix = 'block_{}_'.format(block_id)
# Depthwise
x = DepthwiseConv2D(kernel_size=3, strides=stride, depth_multiplier = depth_multiplier, activation=None, use_bias=True, padding='same', kernel_initializer="he_normal", depthwise_regularizer=regularizers.l2(4e-5), name=prefix + 'depthwise')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'depthwise_BN')(x)
x = ReLU(6., name=prefix + 'depthwise_ReLU')(x)
# Pointwise
x = Conv2D(filters, kernel_size=1, strides=1, padding='same', use_bias=True, activation=None, kernel_initializer="he_normal", kernel_regularizer=regularizers.l2(4e-5), name=prefix + 'project')(x)
x = BatchNormalization(epsilon=1e-3, momentum=0.999, name=prefix + 'project_BN')(x)
x = ReLU(6., name=prefix + 'pointwise_ReLU')(x)
return x
#Create Build
def create_model(rows, cols, channels, lr_initial):
# encoder - input
model_input = keras.Input(shape=(rows, cols, channels), name='input_image')
x = model_input
# model architechture
x = Conv2D(32, kernel_size=3, strides=2, padding='same', use_bias=True, kernel_initializer="he_normal", kernel_regularizer=regularizers.l2(4e-5), name='Conv1')(model_input)
x = mobile_net_block(x, filters=64, stride=1, depth_multiplier=1, block_id=0)
x = mobile_net_block(x, filters=64, stride=1, depth_multiplier=1, block_id=1)
# x = mobile_net_block(x, filters=256, stride=1, depth_multiplier=1, block_id=2)
# x = mobile_net_block(x, filters=512, stride=1, depth_multiplier=1, block_id=3)
# x = mobile_net_block(x, filters=512, stride=1, depth_multiplier=1, block_id=4)
# x = mobile_net_block(x, filters=512, stride=1, depth_multiplier=1, block_id=5)
# x = mobile_net_block(x, filters=512, stride=1, depth_multiplier=1, block_id=6)
# x = mobile_net_block(x, filters=512, stride=1, depth_multiplier=1, block_id=7)
# x = mobile_net_block(x, filters=1024, stride=2, depth_multiplier=1, block_id=8)
# x = mobile_net_block(x, filters=1024, stride=2, depth_multiplier=1, block_id=9)
# Decoder
x = GlobalAveragePooling2D(name='global_average_pool')(x)
x = Dense(3, activation='softmax', use_bias=True, name='Logits')(x)
# create model of MobileNet (for CIFAR-10)
model = Model(inputs=model_input, outputs=x, name='mobilenet_cifar10')
model.compile(optimizer=tf.keras.optimizers.RMSprop(0.01), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
model = create_model(size, size, 1, 0.01)
model.summary()
earlystop = EarlyStopping(monitor='val_loss', patience=30, verbose=1)
history = model.fit(
train_generator,
steps_per_epoch=15,
epochs=25,
validation_data=validation_generator,
validation_steps=5,
verbose=2,
callbacks = [earlystop])
However it still doesn't work. As a result, I'm thinking that what if I use same data for training and testing. The accuracy should have same performance or even better, in fact it doesn't. Could anyone explain that? sorry I don't know why the indentation are all mess up after pasting code on this website.
Solution 1:[1]
Did i got that right, you used the same data for training and testing the model and still got different results? When testing the model with exactly the same data it was trained with, the results should be the exactly same.
Are you running your training in a Jupyter Notebook / Lab or Google Colab environment? If so, try to reset the environment and run all code cells again.
By the way, you code formatting is not correct (missing indentation) and also some of your code is missing (the .fit() call) ;)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | steffensc |
