Пример #1
0
def main(args):
    train_data = args.train_data
    valid_data = args.valid_data
    nb_train_samples = args.train_samples
    nb_validation_samples = args.valid_samples
    model_path = args.model_path
    model_weights_path = args.model_weights_path
    num_classes = args.num_classes
    batch_size = args.batch_size
    epochs = args.epochs
    loss = args.loss
    optimizer = args.optimizer
    img_width, img_height = args.target_size

    if not os.path.isdir(train_data):
        raise EnvironmentError(f'--train-data {train_data} should exist')

    if not os.path.isdir(valid_data):
        raise EnvironmentError(f'--valid-data {valid_data} should exist')

    if K.image_data_format() == 'channels_first':
        input_shape = (3, img_width, img_height)
    else:
        input_shape = (img_width, img_height, 3)

    model_params = {
        'input_tensor': Input(shape=input_shape),
        'classes': num_classes,
        'weights': None,
    }

    print(
        f'Start training mobile net for {epochs} epochs.',
        f'{nb_train_samples} train samples, {nb_validation_samples} valid samples'
    )

    model = MobileNet(**model_params)
    model.compile(loss=loss, optimizer=optimizer, metrics=['accuracy'])

    train_datagen = ImageDataGenerator(rescale=1. / 255)
    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical',
    )

    validation_generator = test_datagen.flow_from_directory(
        valid_data,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='categorical',
    )

    model.fit_generator(train_generator,
                        steps_per_epoch=nb_train_samples // batch_size,
                        epochs=epochs,
                        validation_data=validation_generator,
                        validation_steps=nb_validation_samples // batch_size,
                        verbose=1)

    model.save(model_path)
    model.save_weights(model_weights_path)

    print('Model saved.')
    return 0
Пример #2
0
if earlyStopFlag:
    callbacksList.append(earlyStop)
if reduceLRFlag:
    callbacksList.append(reduce_lr)
if modelCheckpointFlag:
    callbacksList.append(modelCheckpoint)

history = model.fit_generator(
    trainGenerator,
    steps_per_epoch=trainSamplesNumber // batchSize * foldAugment,
    epochs=epochs,
    verbose=1,
    callbacks=callbacksList,
    validation_data=validationGenerator,
    class_weight=getClassWeights(trainGenerator.classes),
    shuffle=True,
    validation_steps=validateSamplesNumber // batchSize)

score = model.evaluate_generator(testGenerator, testSamplesNumber)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

# serialize model to JSON
model_json = model.to_json()
with open(modelFile, "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights(weightsFile)
print("Saved model to disk")
Пример #3
0
lb = LabelBinarizer()
lb.fit(np.asarray(data['primary_microconstituent']))
y = lb.transform(labels)
print('\nLabels Binarized, converting array')


input = np.asarray(processed_imgs)

X_train, X_test, y_train, y_test = train_test_split(
    input, y, test_size=0.1, random_state=42)


model = MobileNet(weights=None, classes = 7)

model.summary()
model.compile(loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy'])
time_callback = TimeHistory()
model.fit(X_train, y_train, epochs = 5, batch_size = 32, validation_data=(X_test, y_test), callbacks=[time_callback])
name = 'results/UHCS_MobileNet_Weights'
score = model.evaluate(X_test, y_test)
print('Test score:', score[0])
print('Test accuracy:', score[1])
model.save_weights(name+'.h5')

times = time_callback.times
file = open('MobileNet.txt', 'w')
file.write('Test score:'+ str(score[0])+'\n')
file.write('Test accuracy:'+ str(score[1])+'\n')
file.write(times)
file.close()
from keras.applications.mobilenet import MobileNet
import sys
sys.path.append('/lfs/1/thaonguyen/PuzzleSolving/')
from train_puzzle import PuzzleModel

model = PuzzleModel(input_shape=(76, 76, 27))
new_model = MobileNet(input_shape=None, include_top=False, weights=None)
model.load_weights('/lfs/1/thaonguyen/PuzzleSolving/WEIGHTS/weights-best.hdf5')

mbnet_layer = model.get_layer('mobilenet_1.00_76')
for layer in new_model.layers[1:]:
    layer.set_weights(mbnet_layer.get_layer(layer.name).get_weights())
#print mbnet_layer.summary()
new_model.save_weights('mobilenet-weights-best.hdf5')