def test(epoch, mode=1): import matplotlib.pyplot as plt from PIL import Image from utils.helper_function import combine_images if mode == 1: num_classes = 10 _, (x_test, y_test) = load_cifar_10() else: num_classes = 100 _, (x_test, y_test) = load_cifar_100() model = CapsNetv1(input_shape=[32, 32, 3], n_class=num_classes, n_route=3) model.load_weights('weights/capsule_weights/capsule-cifar-' + str(num_classes) + 'weights-{:02d}.h5'.format(epoch)) print("Weights loaded, start validation") # model.load_weights('weights/capsule-weights-{:02d}.h5'.format(epoch)) y_pred, x_recon = model.predict([x_test, y_test], batch_size=100) print('-' * 50) # Test acc: 0.7307 print( 'Test acc:', np.sum(np.argmax(y_pred, 1) == np.argmax(y_test, 1)) / y_test.shape[0]) img = combine_images(np.concatenate([x_test[:50], x_recon[:50]])) image = img * 255 Image.fromarray(image.astype(np.uint8)).save("results/real_and_recon.png") print('Reconstructed images are saved to ./results/real_and_recon.png') print('-' * 50) plt.imshow(plt.imread("results/real_and_recon.png", )) plt.show()
def test(epoch, mode=1): import matplotlib.pyplot as plt from PIL import Image from utils.helper_function import combine_images if mode == 1: num_classes =10 _,(x_test,y_test) = load_cifar_10() else: num_classes = 100 _,(x_test,y_test) = load_cifar_100() model = CapsNet(input_shape=[32, 32, 3], n_class=num_classes, n_route=3) model.load_weights('weights/capsule-cifar-'+str(num_classes)+'weights-{:02d}.h5'.format(epoch)) print("Weights loaded, start validation") # model.load_weights('weights/capsule-weights-{:02d}.h5'.format(epoch)) y_pred, x_recon = model.predict([x_test, y_test], batch_size=100) print('-'*50) print('Test acc:', np.sum(np.argmax(y_pred, 1) == np.argmax(y_test, 1))/y_test.shape[0]) img = combine_images(np.concatenate([x_test[:50],x_recon[:50]])) image = img*255 Image.fromarray(image.astype(np.uint8)).save("results/real_and_recon.png") print() print('Reconstructed images are saved to ./results/real_and_recon.png') print('-'*50) plt.imshow(plt.imread("results/real_and_recon.png", )) plt.show()
def train(epochs=200, batch_size=64, mode=1): import numpy as np import os from keras import callbacks from keras.utils.vis_utils import plot_model if mode == 1: num_classes = 10 (x_train, y_train), (x_test, y_test) = load_cifar_10() else: num_classes = 100 (x_train, y_train), (x_test, y_test) = load_cifar_100() model = CapsNetv1(input_shape=[32, 32, 3], n_class=num_classes, n_route=3) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') model.summary() log = callbacks.CSVLogger('results/capsule-cifar-' + str(num_classes) + '-log.csv') tb = callbacks.TensorBoard(log_dir='results/tensorboard-capsule-cifar-' + str(num_classes) + '-logs', batch_size=batch_size, histogram_freq=True) checkpoint = callbacks.ModelCheckpoint( 'weights/capsule-cifar-' + str(num_classes) + 'weights-{epoch:02d}.h5', save_best_only=True, save_weights_only=True, verbose=1) lr_decay = callbacks.LearningRateScheduler( schedule=lambda epoch: 0.001 * np.exp(-epoch / 10.)) plot_model(model, to_file='models/capsule-cifar-' + str(num_classes) + '.png', show_shapes=True) model.compile(optimizer=optimizers.Adam(lr=0.001), loss=[margin_loss, 'mse'], loss_weights=[1., 0.1], metrics={ 'output_recon': 'accuracy', 'output': 'accuracy' }) from utils.helper_function import data_generator generator = data_generator(x_train, y_train, batch_size) # Image generator significantly increase the accuracy and reduce validation loss model.fit_generator(generator, steps_per_epoch=x_train.shape[0] // batch_size, validation_data=([x_test, y_test], [y_test, x_test]), epochs=epochs, verbose=1, max_q_size=100, callbacks=[log, tb, checkpoint, lr_decay])
def test(): num_classes = 10 _, (x_test, y_test) = load_cifar_10() model = ResnetBuilder.build_resnet_18((32, 32, 3), 10) epoch = 44 model.load_weights('weights/resnet_weights/resnet-cifar-' + str(num_classes) + 'weights-{:02d}.h5'.format(epoch)) print("Weights loaded, start validation") # model.load_weights('weights/capsule-weights-{:02d}.h5'.format(epoch)) y_pred = model.predict(x_test, batch_size=100) print('-' * 50) # Test acc: 0.8231 print( 'Test acc:', np.sum(np.argmax(y_pred, 1) == np.argmax(y_test, 1)) / y_test.shape[0])
def train(epochs=200,batch_size=64,mode=1): import numpy as np import os from keras import callbacks from keras.utils.vis_utils import plot_model if mode==1: num_classes = 10 (x_train,y_train),(x_test,y_test) = load_cifar_10() else: num_classes = 100 (x_train,y_train),(x_test,y_test) = load_cifar_100() model = CapsNetv1(input_shape=[32, 32, 3], n_class=num_classes, n_route=3) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') model.summary() log = callbacks.CSVLogger('results/capsule-cifar-'+str(num_classes)+'-log.csv') tb = callbacks.TensorBoard(log_dir='results/tensorboard-capsule-cifar-'+str(num_classes)+'-logs', batch_size=batch_size, histogram_freq=True) checkpoint = callbacks.ModelCheckpoint('weights/capsule-cifar-'+str(num_classes)+'weights-{epoch:02d}.h5', save_best_only=True, save_weights_only=True, verbose=1) lr_decay = callbacks.LearningRateScheduler(schedule=lambda epoch: 0.001 * np.exp(-epoch / 10.)) plot_model(model, to_file='models/capsule-cifar-'+str(num_classes)+'.png', show_shapes=True) model.compile(optimizer=optimizers.Adam(lr=0.001), loss=[margin_loss, 'mse'], loss_weights=[1., 0.1], metrics={'output_recon':'accuracy','output':'accuracy'}) from utils.helper_function import data_generator generator = data_generator(x_train,y_train,batch_size) model.fit_generator(generator, steps_per_epoch=x_train.shape[0] // batch_size, validation_data=([x_test, y_test], [y_test, x_test]), epochs=epochs, verbose=1, max_q_size=100, callbacks=[log,tb,checkpoint,lr_decay])
def train(epochs=150, batch_size=32, mode=1): import numpy as np import os from keras import callbacks from keras.utils.vis_utils import plot_model import keras from keras.preprocessing.image import ImageDataGenerator if mode == 1: num_classes = 10 (x_train, y_train), (x_test, y_test) = load_cifar_10() else: num_classes = 100 (x_train, y_train), (x_test, y_test) = load_cifar_100() datagen = ImageDataGenerator( featurewise_center=False, # set input mean to 0 over the dataset samplewise_center=False, # set each sample mean to 0 featurewise_std_normalization= False, # divide inputs by std of the dataset samplewise_std_normalization=False, # divide each input by its std zca_whitening=False, # apply ZCA whitening rotation_range= 0, # randomly rotate images in the range (degrees, 0 to 180) width_shift_range= 0.1, # randomly shift images horizontally (fraction of total width) height_shift_range= 0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=True, # randomly flip images vertical_flip=False) # randomly flip images # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) ''' Total params: 21,311,754 Trainable params: 21,296,522 Non-trainable params: 15,232 ''' model = ResnetBuilder.build_resnet_18((32, 32, 3), 10) model.summary() # Let's train the model using RMSprop model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') model.summary() log = callbacks.CSVLogger('results/resnet-cifar-' + str(num_classes) + '-log.csv') tb = callbacks.TensorBoard(log_dir='results/tensorboard-resnet-cifar-' + str(num_classes) + '-logs', batch_size=batch_size, histogram_freq=True) checkpoint = callbacks.ModelCheckpoint( 'weights/resnet-cifar-' + str(num_classes) + 'weights-{epoch:02d}.h5', save_best_only=True, save_weights_only=True, verbose=1) lr_reducer = callbacks.ReduceLROnPlateau(factor=np.sqrt(0.1), cooldown=0, patience=5, min_lr=0.5e-6) plot_model(model, to_file='models/resnet-cifar-' + str(num_classes) + '.png', show_shapes=True) model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, validation_data=(x_test, y_test), epochs=epochs, verbose=1, max_q_size=100, callbacks=[log, tb, checkpoint, lr_reducer])