Ejemplo n.º 1
0
                                                       TEST_SET[-11:-7],
                                                       BATCH_SIZE, EPOCHS,
                                                       INPUT_FRAME_SIZE)
    best_model = ModelCheckpoint(best_model_file,
                                 monitor='val_loss',
                                 verbose=1,
                                 save_best_only=True)

    print("*************************************")
    print("Fitting model")
    print("*************************************")

    model.fit(
        x_train,
        y_train,
        batch_size=BATCH_SIZE,
        epochs=EPOCHS,
        verbose=1,
        callbacks=[best_model],  # this callback can be de-activated
        validation_data=(x_test, y_test))

    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,  # randomly shift images horizontally (fraction of total width)
Ejemplo n.º 2
0
        X_train = X[:tmp]
        Y_train = Y[:tmp]
        X_val = X[tmp:]
        Y_val = Y[tmp:]

        for epoch in range(nb_epoch):
            t1 = time.time()
            print("### Model Fitting.. ###")
            print('epoch = {} / {}'.format(epoch+1, nb_epoch))
            print('check point = {}'.format(epoch))

            # for no augmentation case
            hist = model.fit(X_train, Y_train,
                             validation_data=(X_val, Y_val),
                             batch_size=batch_size,
                             #initial_epoch=epoch,
                             callbacks=[reduce_lr],
                             shuffle=True
                             )
            t2 = time.time()
            print(hist.history)
            print('Training time for one epoch : %.1f' % ((t2 - t1)))
            train_acc = hist.history['categorical_accuracy'][0]
            train_loss = hist.history['loss'][0]
            val_acc = hist.history['val_categorical_accuracy'][0]
            val_loss = hist.history['val_loss'][0]

            
            nsml.report(summary=True, step=epoch, epoch_total=nb_epoch, loss=train_loss, acc=train_acc, val_loss=val_loss, val_acc=val_acc)
            nsml.save(epoch)
        print('Total training time : %.1f' % (time.time() - t0))
layers = ['new_pool_1','new_batch_1','new_dense_1','new_batch_2','new_drop_2','new_dene_2']
model_1.get_layer('new_dene_2').set_weights([np.array(weights[10],dtype=np.float32),np.zeros([3],dtype=np.float32)])
model_1.get_layer('new_dense_1').set_weights([np.array(weights[4],dtype=np.float32),np.zeros([512],dtype=np.float32)])

# the first 249 layers and unfreeze the rest:
for layer in model_1.layers[:249]:
   layer.trainable = False
for layer in model_1.layers[249:]:
   layer.trainable = True

chkp = ModelCheckpoint('skin_2.cancer.models.best.hdf5',verbose=1)
# print(weights[10])
# print(model_1.get_layer('new_dene_2').get_weights()[0])

model_1.load_weights('skin_2.cancer.models.best.hdf5')

model_1.fit(bottle_neck_train_set_1,train_targets,batch_size=64,epochs=300,verbose=1
          ,callbacks=[chkp],shuffle=True,validation_data=(bottle_neck_valid_set_1,valid_targets))

model_1.load_weights('skin_2.cancer.models.best.hdf5')
_pred = np.argmax(model_1.predict(bottle_neck_train_set_1),axis=1)
true_pred = np.argmax(train_targets,axis=1)
true=[]
for index in range(len(_pred)):
      if(true_pred[index]==_pred[index]):
          true.append(1)
      else:
        true.append(0)
print("Test Set Accuracy {}%".format((sum(true)/len(true))*100))
Ejemplo n.º 4
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 = Xception(weights=None, classes = 7)

model.summary()
model.compile(loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy'])

model.fit(X_train, y_train, epochs = 5, batch_size = 32, validation_data=(X_test, y_test))
name = 'results/UHCS_Xception_Weights'
score = model.evaluate(X_test, y_test)
print('Test score:', score[0])
print('Test accuracy:', score[1])
model.save_weights(name+'.h5')

file = open('Xception.txt', 'w')
file.write('Test score:', score[0])
file.write('Test accuracy:', score[1])
file.close()
Ejemplo n.º 5
0
def main(args):

    # hyper parameters
    batch_size = 16
    num_classes = 102
    epochs = 100

    # Instantiate model
    model = Xception(include_top=True, weights=None, classes=num_classes)

    # prepare data
    x_train = np.load(os.path.join(current_directory, 'x_train.npy'))
    y_train = np.load(os.path.join(current_directory, 'y_train.npy'))
    x_test = np.load(os.path.join(current_directory, 'x_test.npy'))
    y_test = np.load(os.path.join(current_directory, 'y_test.npy'))

    # summary of the model
    model.summary()

    # compile model
    model.compile(
        loss=categorical_crossentropy,
        optimizer=Adadelta(),
        metrics=['accuracy']
    )

    # learning section
    hist = model.fit(
        x_train,
        y_train,
        batch_size=batch_size,
        epochs=epochs,
        verbose=1,
        validation_data=(x_test, y_test)
    )

    # evaluation section
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

    # save graphs
    acc = hist.history['acc']
    val_acc = hist.history['val_acc']
    loss = hist.history['loss']
    val_loss = hist.history['val_loss']

    plt.plot(range(epochs), loss, marker='.', label='acc')
    plt.plot(range(epochs), val_loss, marker='.', label='val_acc')
    plt.legend(loc='best')
    plt.grid()
    plt.xlabel('epoch')
    plt.ylabel('acc')
    plt.savefig(os.path.join(current_directory, 'acc_xception.png'))
    plt.clf()

    plt.plot(range(epochs), acc, marker='.', label='loss')
    plt.plot(range(epochs), val_acc, marker='.', label='val_loss')
    plt.legend(loc='best')
    plt.grid()
    plt.xlabel('epoch')
    plt.ylabel('loss')
    plt.savefig(os.path.join(current_directory, 'loss_xception.png'))
    plt.clf()
Ejemplo n.º 6
0
def train(epochs):
    image_size = (299, 299)
    # variables to hold features and labels
    features = []
    labels = []

    # default setting in keras models
    class_count = 1000
    X_test = []
    name_test = []

    trainData = np.loadtxt("./train.txt", dtype="str", delimiter=' ')
    for k in range(len(trainData)):
        aLine = trainData[k]
        image_path = aLine[0]
        label = int(aLine[1])
        ground_truth = np.zeros(class_count, dtype=np.float32)
        ground_truth[label] = 1

        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        labels.append(ground_truth)
        features.append(x[0])

    trainData = np.loadtxt("./val.txt", dtype="str", delimiter=' ')
    for k in range(len(trainData)):
        aLine = trainData[k]
        image_path = aLine[0]
        label = int(aLine[1])
        ground_truth = np.zeros(class_count, dtype=np.float32)
        ground_truth[label] = 1

        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        labels.append(ground_truth)
        features.append(x[0])

    testData = np.loadtxt("./test.txt", dtype="str", delimiter=' ')
    for k in range(len(testData)):
        aLine = testData[k]
        image_path = aLine
        img = image.load_img(image_path, target_size=image_size)
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        X_test.append(x[0])
        name_test.append(image_path)

    X_train = features
    y_train = labels

    X_train = np.array(X_train)
    Y_train = np.array(y_train)

    # test image
    X_test = np.array(X_test)

    # Use Xception
    model = Xception(include_top=True, weights='imagenet', classes=class_count)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    model.fit(X_train, Y_train, epochs=epochs, verbose=1, validation_split=0.3)

    Y_pred = model.predict(X_test)

    f = open('project2_08573584.txt', 'w')
    for i in range(len(name_test)):
        predict = Y_pred[i].argmax(axis=0)
        f.write(str(predict) + '\n')
    f.close()
Ejemplo n.º 7
0
    # Load our model
    # model = densenet169_model(img_rows=img_rows, img_cols=img_cols, color_type=channel, num_classes=num_classes)

    # load keras model
    model = Xception(weights=None, classes=10)
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()

    # Start Fine-tuning
    model.fit(
        X_train,
        Y_train,
        batch_size=batch_size,
        epochs=nb_epoch,
        shuffle=True,
        verbose=1,
        validation_data=(X_valid, Y_valid),
    )

    # Make predictions
    predictions_valid = model.predict(X_valid,
                                      batch_size=batch_size,
                                      verbose=1)

    # Cross-entropy loss score
    score = log_loss(Y_valid, predictions_valid)
Ejemplo n.º 8
0
# **Using RMSprop optimizer, mean absolute error with metrics, and mean square erro with loss**

# In[ ]:

opt = RMSprop(lr=0.0001)
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['mae'])

# **Puting the model for fit**
#
# **NOTE: The number of epochs is set to 100**

# 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[ ]:
Ejemplo n.º 9
0
height = 71
width = 71
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.