weights='imagenet', input_tensor=None, input_shape=None) elif args.model == "squeezenet": import sys sys.path.append("./keras-squeezenet") from keras_squeezenet import SqueezeNet model = SqueezeNet(input_tensor=None, input_shape=None, weights='imagenet', classes=1000) else: exit() if args.compress == True: model.save("model_original.hdf5") import sys sys.path.append("./keras_compressor/keras_compressor") import subprocess subprocess.call( "python ./keras_compressor/bin/keras-compressor.py model_original.hdf5 model_compressed.hdf5 --log-level DEBUG", shell=True) from keras.models import load_model model = load_model("model_compressed.hdf5") model.summary() model.save_weights("model.hdf5") model.summary() model.save_weights("model.hdf5") with open('model.json', 'w') as f: f.write(model.to_json())
# In[ ]: network_history = model.fit(x_train, y_train, batch_size=32, epochs=100, verbose=1, validation_data=(x_val, y_val)) # ### Save the Model Trained # In[ ]: #model.save('/content/drive/My Drive/ColabNotebooks/AllmodeloRMSpropXception.h5') model.save('/content/drive/My Drive/ColabNotebooks/Xception/modelXception.h5') # ### Load the Model Trained # In[ ]: from keras.models import load_model #model = load_model('/content/drive/My Drive/ColabNotebooks/AllmodeloRMSpropXception.h5') model = load_model( '/content/drive/My Drive/ColabNotebooks/Xception/modelXception.h5') # ## Predict the Model Trained # **Show the three better images and the three wrong image of the test set**
def top_5(y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=5) if __name__ == '__main__': batch_size = 16 train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory( 'data', target_size=(224, 224), batch_size=batch_size, class_mode='categorical', shuffle=True) model = Xception(include_top=True, weights=None, classes=10) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', top_5]) model.fit_generator(train_generator, steps_per_epoch=125 // batch_size, epochs=10) model.save('xception10.h5')
num_classes = 100 start = datetime.datetime.now() with tf.device('/gpu:0'): model = Xception(weights=None, input_shape=(height, width, 3), classes=num_classes) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') # Generate dummy data. x = np.random.random((num_samples, height, width, 3)) y = np.random.random((num_samples, num_classes)) model.fit(x, y, epochs=3, batch_size=16) model.save('my_model.h5') end = datetime.datetime.now() time_delta = end - start print('GPU 처리시간 : ', time_delta) start = datetime.datetime.now() with tf.device('/cpu:0'): model = Xception(weights=None, input_shape=(height, width, 3), classes=num_classes) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') # Generate dummy data. x = np.random.random((num_samples, height, width, 3)) y = np.random.random((num_samples, num_classes))