Ejemplo n.º 1
0
def main():
    original_train = pd.read_csv(args.train_dataset)
    test = pd.read_csv(args.test_dataset)

    print("Original Training Dataset ", original_train.shape)
    print("Test Dataset ", test.shape)

    augment_train_images(args.train_images_folder)

    augmented_train = pd.read_csv('train_augmented.csv')
    print(augmented_train.shape)
    print(augmented_train.head())

    print("Original Dataset ")
    print(original_train['target'].value_counts())
    print("Augmented Dataset ")
    print(augmented_train['target'].value_counts())

    train_fnames = os.listdir(args.train_images_folder)
    test_fnames = os.listdir(args.test_images_folder)

    train_fnames = [x for x in train_fnames if x.endswith('.jpg')]
    test_fnames = [x for x in test_fnames if x.endswith('.jpg')]

    print("Training Images Count: ", len(train_fnames))
    print("Test Images Count: ", len(test_fnames))

    train_data, train_labels = utils.load_training_data(
        train_fnames, augmented_train, args.train_images_folder)
    train_data = np.array(train_data)
    train_labels = np.array(train_labels)
    print("Shape of training data: ", train_data.shape)
    print("Shape of training labels: ", train_labels.shape)

    le = LabelEncoder()
    train_labels = le.fit_transform(train_labels)
    np.save('classes.npy', le.classes_)

    X_train, X_val, y_train, y_val = train_test_split(
        train_data,
        train_labels,
        test_size=args.test_split_size,
        random_state=42)
    y_train = to_categorical(y_train, num_classes=8)
    y_val = to_categorical(y_val, num_classes=8)

    print("Shape of test_x: ", X_train.shape)
    print("Shape of train_y: ", y_train.shape)
    print("Shape of test_x: ", X_val.shape)
    print("Shape of test_y: ", y_val.shape)

    train_datagenerator = ImageDataGenerator(rescale=1. / 255)

    val_datagenerator = ImageDataGenerator(rescale=1. / 255)

    train_datagenerator.fit(X_train)
    val_datagenerator.fit(X_val)

    num_classes = 8
    model = models.vgg_model(num_classes)

    print(model.summary())

    epochs = 30
    model.compile(optimizer='adam',
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    history = model.fit(X_train,
                        y_train,
                        batch_size=30,
                        epochs=epochs,
                        validation_data=(X_val, y_val))

    acc = history.history['accuracy']
    val_acc = history.history['val_accuracy']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    epochs = range(epochs)

    plt.figure(1)
    plt.plot(epochs, acc)
    plt.plot(epochs, val_acc)
    plt.title('Training and validation accuracy')
    plt.savefig('statics/accuracy.png')
    plt.show()

    plt.figure(2)
    plt.plot(epochs, loss)
    plt.plot(epochs, val_loss)
    plt.title('Training and validation loss')
    plt.savefig('statics/validation.png')
    plt.show()

    model.save('dance-form.h5')
Ejemplo n.º 2
0
    'h': h,
    'w': w,
    'z': z,
    'c': 1,
    'learning_rate': 5e-5,
    'optimizer': 'Adam',
    'loss': 'categorical_crossentropy',
    'nbepoch': 2000,
    'num_classes': num_classes,
    'nb_filters': 32,
    'max_patience': 30,
    'stride': 2,
}

#model = models.model(params_train)
model = models.vgg_model(params_train)
model.summary()

# path to weights
path2weights = weightfolder + "/weights.hdf5"

#%%

print('training in progress ...')

# checkpoint settings
#checkpoint = ModelCheckpoint(path2weights, monitor='val_loss', verbose=0, save_best_only='True',mode='min')

# load last weights
if pre_train:
    if os.path.exists(path2weights) and pre_train:
#trainImageList, trainLabelList, testImageList, testLabelList = \
#    ld.loadAllData(dataPath, imageSize, labelSize, trainLength, testLength) # image
#dataFolder = './numpyTenClass'
#files = ['trainImageList.npy', 'trainLabelList.npy', 'testImageList.npy', 'testLabelList.npy']
#trainImageList, trainLabelList, testImageList, testLabelList = ld.loadNumpyData(dataFolder, files)

batchFolder = '../data_merge_few_class_3000'
testsetFolder = '../data_merge_few_class_origin'
testImageList, testLabelList = ld.loadBatchData(testsetFolder,
                                                defines.IMAGE_SIZE,
                                                defines.LABEL_SIZE, 100)
# make model, b1/b2는 구조 print용
util.showProcess('Model Generating')

#m1 = models.model(imageSize, labelSize)
m1 = models.vgg_model(imageSize, labelSize)

models.saveModelDescription(m1, modelImagePath, False)

# Load Weight
if isLoadWeight == 1:
    util.showProcess('Loading Weight')
    models.loadWeight(m1, weightPath)

# Train
util.showProcess('Train M1')
#accList = []

trainAcc = []
trainLoss = []