height = 224 width = 224 num_classes = 1000 # Instantiate the base model (or "template" model). # We recommend doing this with under a CPU device scope, # so that the model's weights are hosted on CPU memory. # Otherwise they may end up hosted on a GPU, which would # complicate weight sharing. with tf.device('/cpu:0'): model = Xception(weights=None, input_shape=(height, width, 3), classes=num_classes) # Replicates the model on 8 GPUs. # This assumes that your machine has 8 available GPUs. parallel_model = multi_gpu_model(model, gpus=8) parallel_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)) # This `fit` call will be distributed on 8 GPUs. # Since the batch size is 256, each GPU will process 32 samples. parallel_model.fit(x, y, epochs=20, batch_size=256) # Save model via the template model (which shares the same weights): model.save('my_model.h5')
from keras.utils import to_categorical from keras import losses,metrics from keras.callbacks import ModelCheckpoint import Dataset_mobile import os if __name__ == '__main__': save_dir = os.path.join(os.getcwd(), 'saved_models') model_name = 'xceptionMobile_wood_model.h5' model_path = os.path.join(save_dir, model_name) x_train, y_train, x_test, y_test, dictionary = Dataset_mobile.read_data() x_train_resized = Dataset_mobile.resize_imgs(x_train) x_test_resized = Dataset_mobile.resize_imgs(x_test) y_train = to_categorical(y_train, num_classes=2) y_test = to_categorical(y_test, num_classes=2) model = Xception(include_top=True, weights=None, classes=2) opt = Adam(lr=5e-6) model.compile(optimizer=opt, loss=losses.categorical_crossentropy, metrics=[metrics.categorical_accuracy]) model.fit(x_train_resized,y_train,epochs=20,batch_size=6) model.save(model_path) score1 = model.evaluate(x_train_resized, y_train, batch_size=6) score2 = model.evaluate(x_test_resized, y_test, batch_size=6) print(score1) print(score2)
target_size=(img_height, img_width), batch_size=batch_size, shuffle=True, class_mode='categorical') print(train_generator.class_indices) # validation_generator = datagen.flow_from_directory(validation_data_dir, # target_size=(img_width, img_height), # batch_size=batch_size, # shuffle=True, class_mode='categorical') # train for epoch in range(1, epochs + 1): print('Epoch: ', epoch) progbar = generic_utils.Progbar(nb_train_samples) for i in range(1, steps + 1): x_batch, y_batch = train_generator.next() x_batch = np.array( [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in x_batch]) loss, train_acc = model.train_on_batch(x_batch, y_batch) progbar.add(x_batch.shape[0], values=[('train loss', loss), ('train acc', train_acc)]) # saving model m_name = 'epoch{}-train_loss{:.3f}-train_acc{:.3f}.h5'.format( epoch, loss, train_acc) print('Saving ', m_name) if not os.path.exists(saving_path): os.makedirs(saving_path) model.save(os.path.join(saving_path, m_name))
print("GPU 시간 측정 시작 !!!") start1 = 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('./model/time_check1.h5') end1 = datetime.datetime.now() time_delta1 = end1 - start1 print("GPU 시간 측정 끝 !!!") print("CPU 시간 측정 시작 !!!") start2 = 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))
width = 71 num_classes = 100 start = datetime.datetime.now() with tf.device('/gpu:3'): model = Xception(weights=None, input_shape=(height, width, 3), classes=num_classes) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') # Generete 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) import tensorflow as tf from keras.applications import Xception from keras.utils import multi_gpu_model import numpy as np import datetime num_samples = 100 height = 71 width = 71 num_classes = 100