def main():
    args = option()

    # show information on the process ID
    print("[INFO process ID: {}".format(os.getpid()))

    # load training data and testing data 
    trainX, trainY = load_data_train(args["dataset"])
    testX, testY = load_data_test(args["dataset"])
    
    # reshape data matrix
    if K.image_data_format() == "channels_first":
        trainX = trainX.reshape(trainX.shape[0],3,32,32)
        testX = testX.reshape(testX.shape[0],3,32,32)
    else:
        trainX = trainX.reshape(trainX.shape[0],32,32,3)
        testX = testX.reshape(testX.shape[0],32,32,3)
    
    # scale trainX, testX into range [0,1]
    trainX = trainX.astype("float") / 255.0
    testX = testX.astype("float") / 255.0

    # convert labels as vector 
    lb = LabelBinarizer()
    trainY = lb.fit_transform(trainY)
    testY  = lb.fit_transform(testY)

    # initialize the label names for the CIFAR-10 dataset
    labelNames = ["airplane", "automobile", "bird", "cat", "deer",
                    "dog", "frog", "horse", "ship", "truck"]

    # initialize the optimizer and model
    print("[INFO] compiling model...")
    opt = SGD(lr=0.01, momentum=0.9, nesterov=True)
    model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10)
    model.compile(loss="categorical_crossentropy", optimizer=opt,
                metrics=["accuracy"])
    
    # construct set of callbacks
    figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])

    jsonPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
    callbacks = [TrainingMonitor(figPath, jsonPath=jsonPath)]

    # train the network
    print("[INFO] training network...")
    H = model.fit(trainX, trainY, validation_data=(testX, testY),
                            batch_size=64, epochs=100, callbacks=callbacks, verbose=1)
    """                 
Exemple #2
0
def callbacks_func(model_out, start_epoch, plotPath, jsonPath):

    callbacks = [
        EpochCheckpoint(model_out, every=5, startAt=start_epoch),
        TrainingMonitor(plotPath, jsonPath=jsonPath, startAt=start_epoch),
        #ReduceLROnPlateau(monitor = "val_loss",
        #             factor=np.sqrt(0.1),
        #            cooldown=2,
        #           patience=5,
        #          epsilon = 1e-04,
        #         min_lr=0.5e-6,
        #        verbose = 1),
        #LearningRateScheduler(lr_schedule)
    ]
    return callbacks
def model_eval(model,
               train_path,
               val_path,
               label_path,
               input_size,
               batch_size,
               epochs=1,
               name=None):
    train_files = [
        f for f in os.listdir(train_path)
        if os.path.isfile(os.path.join(train_path, f))
    ]
    val_files = [
        f for f in os.listdir(val_path)
        if os.path.isfile(os.path.join(val_path, f))
    ]

    train_steps = np.ceil(float(len(train_files)) / float(batch_size))

    train_gen = generator(train_path, label_path, input_size, batch_size)
    val_gen = generator(val_path, label_path, input_size, batch_size)
    val_steps = np.ceil(float(len(val_files)) / float(batch_size))
    checkpoint = ModelCheckpoint('/tmp/weights.hdf5',
                                 monitor="val_loss",
                                 save_best_only=True,
                                 verbose=1)
    base = f'models/{name}'
    if not os.path.exists(base):
        os.mkdir(base)
    figpath = base + '/model.png'
    jsonpath = base + '/history.json'
    trainmonitor = TrainingMonitor(figpath, jsonPath=jsonpath)
    callbacks = [checkpoint, trainmonitor]
    history = model.fit_generator(generator=train_gen,
                                  validation_data=val_gen,
                                  steps_per_epoch=train_steps,
                                  validation_steps=val_steps,
                                  epochs=epochs,
                                  verbose=1,
                                  callbacks=callbacks)
    if not None:
        model.save('models/' + name + '.h5')
    return model, history
Exemple #4
0
X_train -= mean
X_test -= mean

label_binarizer = LabelBinarizer()
y_train = label_binarizer.fit_transform(y_train)
y_test = label_binarizer.transform(y_test)

augmenter = ImageDataGenerator(width_shift_range=0.1,
                               height_shift_range=0.1,
                               horizontal_flip=True,
                               fill_mode='nearest')

figure_path = os.path.sep.join([arguments['output'], f'{os.getpid()}.png'])
json_path = os.path.sep.join([arguments['output'], f'{os.getpid()}.json'])
callbacks = [
    TrainingMonitor(figure_path, json_path=json_path),
    LearningRateScheduler(poly_decay)
]

print('[INFO] Compiling model...')
optimizer = SGD(lr=INIT_LR, momentum=0.9)
model = MiniGoogLeNet.build(width=32, height=32, depth=3, classes=10)
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

print('[INFO] Training network...')
model.fit_generator(augmenter.flow(X_train, y_train, batch_size=64),
                    validation_data=(X_test, y_test),
                    steps_per_epoch=len(X_train) // 64,
                    epochs=NUM_EPOCHS,
Exemple #5
0
trainX = trainX.astype("float") / 255.0
testX = testX.astype("float") / 255.0

#Convert the labels from integers to vectores
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

#Initialize the label names for the CIFAR-10 dataset
labelNames = ["airplane", "automobile", "bird", "cat", "deer",
			"dog", "frog", "horse", "ship", "truck"]

#Initialize the SGD optimizer, but without any learning rate decay
print("[INFO] compiling model....")
opt = SGD(lr=0.01, momentum=0.9, nesterov=True)
model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10)
model.compile(loss="categorical_crossentropy", optimizer=opt,
	metric=["accuracy"])

#Construct the set of callbacks
figPath = os.path.sep.join([args["output"], "{}.png".format(
	os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(
	os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath=jsonPath)]

#Train the network
print("[INFO] training network...")
model.fit(trainX, trainY, validation_data=(testX, testY),
	batch_size=64, epochs=100, callbacks=callbacks, verbose=1)
trainX -= mean
testX -= mean
# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)
# construct the image generator for data augmentation
aug = ImageDataGenerator(width_shift_range=0.1,
                         height_shift_range=0.1,
                         horizontal_flip=True,
                         fill_mode="nearest")
# construct the set of callbacks
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
callbacks = [
    TrainingMonitor(figPath, jsonPath=jsonPath),
    LearningRateScheduler(poly_decay)
]
# initialize the optimizer and model
print("[INFO] compiling model...")
opt = SGD(lr=INIT_LR, momentum=0.9)
model = MiniGoogLeNet.build(width=32, height=32, depth=3, classes=10)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])
# train the network
print("[INFO] training network...")
model.fit_generator(aug.flow(trainX, trainY, batch_size=64),
                    validation_data=(testX, testY),
                    steps_per_epoch=len(trainX) // 64,
                    epochs=NUM_EPOCHS,
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float') / 255.0
X_test = X_test.astype('float') / 255.0

label_binarizer = LabelBinarizer()
y_train = label_binarizer.fit_transform(y_train)
y_test = label_binarizer.transform(y_test)

label_names = [
    'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
    'ship', 'truck'
]
print('[INFO] Compiling model...')
optimizer = SGD(lr=0.01, momentum=0.9, nesterov=True)
model = MiniVGGNet.build(width=32, height=32, depth=3, classes=10)
model.compile(loss='categorical_crossentropy',
              optimizer=optimizer,
              metrics=['accuracy'])

figure_path = os.path.sep.join([arguments['output'], f'{os.getpid()}.png'])
json_path = os.path.sep.join([arguments['output'], f'{os.getpid()}.json'])
callbacks = [TrainingMonitor(figure_path, json_path)]

print('[INFO] Training network...')
model.fit(X_train,
          y_train,
          validation_data=(X_test, y_test),
          batch_size=64,
          epochs=100,
          callbacks=callbacks)
Exemple #8
0
if arguments['model'] is None:
    print('[INFO] Compiling model...')
    model = DeeperGoogLeNet.build(width=64, height=64, depth=3, classes=config.NUM_CLASSES, regularization=0.0002)
    optimizer = Adam(lr=1e-3)
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
else:
    print(f'[INFO] Loading {arguments["model"]}...')
    model = load_model(arguments['model'])

    print(f'[INFO] Old learning rate: {K.get_value(model.optimizer.lr)}')
    K.set_value(model.optimizer.lr, 1e-5)

    print(f'[INFO] New learning rate: {K.get_value(model.optimizer.lr)}')

callbacks = [
    EpochCheckpoint(arguments['checkpoints'], every=5, start_at=arguments['start_epoch']),
    TrainingMonitor(config.FIGURE_PATH, json_path=config.JSON_PATH, start_at=arguments['start_epoch'])
]

model.fit_generator(train_generator.generator(),
                    steps_per_epoch=train_generator.num_images // 64,
                    validation_data=val_generator.generator(),
                    validation_steps=val_generator.num_images // 64,
                    epochs=20,
                    max_queue_size=10,
                    callbacks=callbacks,
                    verbose=1)

train_generator.close()
val_generator.close()
# shape 为 (32,32,3)
mean = np.mean(x_train, axis=0)
x_train -= mean
x_test -= mean
# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(y_train)
testY = lb.transform(y_test)
# construct the image generator for data augmentation
aug = ImageDataGenerator(width_shift_range=0.1,
                         height_shift_range=0.1,
                         horizontal_flip=True,
                         fill_mode="nearest")
# construct the set of callbacks
callbacks = [
    TrainingMonitor(fig_path='resnet56_cifar10.jpg',
                    json_path='resnet56_cifar10.json'),
    LearningRateScheduler(poly_decay)
]
# initialize the optimizer and model (ResNet-56)
print("[INFO] compiling model...")
opt = SGD(lr=INIT_LR, momentum=0.9)
model = ResNet.build(32,
                     32,
                     3,
                     10, (9, 9, 9), (64, 64, 128, 256),
                     regularization=0.0005)
model.compile(loss="categorical_crossentropy",
              optimizer=opt,
              metrics=["accuracy"])
# train the network
print("[INFO] training network...")
Exemple #10
0
# initialize the training and validation dataset generators
trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 128, aug=aug,
                                preprocessors=[pp, mp, iap], classes=2)
valGen = HDF5DatasetGenerator(config.VAL_HDF5, 128,
                              preprocessors=[sp, mp, iap], classes=2)
# initialize the optimizer
print("[INFO] compiling model...")
opt = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, depth=3,
                      classes=2, reg=0.0002)
model.compile(loss="binary_crossentropy", optimizer=opt,
              metrics=["accuracy"])
# construct the set of callbacks
path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(
    os.getpid())])
callbacks = [TrainingMonitor(path)]
# train the network
model.fit_generator(
    trainGen.generator(),
    steps_per_epoch=trainGen.numImages // 128,
    validation_data=valGen.generator(),
    validation_steps=valGen.numImages // 128,
    epochs=75,
    max_queue_size=128 * 2,
    callbacks=callbacks, verbose=1)
# save the model to file
print("[INFO] serializing model...")
model.save(config.MODEL_PATH, overwrite=True)
# close the HDF5 datasets
trainGen.close()
valGen.close()
Exemple #11
0
                              aug=aug,
                              preprocessors=[sp, mp, iap],
                              classes=2)

print('[INFO] : Compiling model....!')
opt = Adam(lr=1e-3)
model = AlexNet.build(width=227, height=227, depth=3, classes=2, reg=0.0002)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['acc'])

print('[INFO] : Construct the set of callbacks...!')
path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())])
print(path)
from slacknotify.slacknotify import SendNotification

slackNoticaiton = SendNotification()
callbacks = [TrainingMonitor(path, notificaiton=slackNoticaiton)]

try:
    slackNoticaiton.train_start()

    model.fit_generator(trainGen.generator(),
                        steps_per_epoch=trainGen.numImages / config.BATCH_SIZE,
                        validation_data=valGen.generator(),
                        validation_steps=valGen.numImages / config.BATCH_SIZE,
                        epochs=config.EPOCHS,
                        max_queue_size=10,
                        callbacks=callbacks,
                        verbose=1)
except Exception as ex:
    slackNoticaiton.trainCrash(ex)
    print("[INFO] loading {}...".format(args["model"]))
    model = load_model(args["model"])

    # update the learning rate
    print("[INFO] old learning rate: {}".format(K.get_value(
        model.optimizer.lr)))
    K.set_value(model.optimizer.lr, 1e-3)
    print("[INFO] new learning rate: {}".format(K.get_value(
        model.optimizer.lr)))

# construct the set of callbacks
figPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.png"])
jsonPath = os.path.sep.join([config.OUTPUT_PATH, "vggnet_emotion.json"])
callbacks = [
    EpochCheckpoint(args["checkpoints"], every=5, startAt=args["start_epoch"]),
    TrainingMonitor(figPath, jsonPath=jsonPath, startAt=args["start_epoch"])
]

# train the network
model.fit_generator(trainGen.generator(),
                    steps_per_epoch=trainGen.numImages // config.BATCH_SIZE,
                    validation_data=valGen.generator(),
                    validation_steps=valGen.numImages // config.BATCH_SIZE,
                    epochs=15,
                    max_queue_size=config.BATCH_SIZE * 2,
                    callbacks=callbacks,
                    verbose=1)

# close the databases
trainGen.close()
valGen.close()
Exemple #13
0
                         3,
                         10, (9, 9, 9), (64, 64, 128, 256),
                         regularization=0.0005)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])
else:
    print(f'[INFO[ Loading {arguments["model"]}...')
    model = load_model(arguments['model'])

    print(f'[INFO] Old learing rate: {K.get_value(model.optimizer.lr)}')
    K.set_value(model.optimizer.lr, 1e-5)
    print(f'[INFO] New learning rate: {K.get_value(model.optimizer.lr)}')

callbacks = [
    EpochCheckpoint(arguments['checkpoints'],
                    every=5,
                    start_at=arguments['start_epoch']),
    TrainingMonitor('output/resnet56_cifar10.png',
                    json_path='output/resnet56_cifar10.json',
                    start_at=arguments['start_epoch'])
]

print('[INFO] Training network...')
model.fit_generator(augmenter.flow(X_train, y_train, batch_size=64),
                    validation_data=(X_test, y_test),
                    steps_per_epoch=len(X_train) // 64,
                    epochs=100,
                    callbacks=callbacks,
                    verbose=1)
Exemple #14
0
    model = DeeperGoogLeNet.build(64, 64, 3, config.NUM_CLASSES, reg=0.0002)
    opt = SGD(lr=0.01)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])
else:
    print("[INFO] loading model {}........".format(args["model"]))
    model = load_model(args["model"])
    print("[INFO] old learning rate: {}".format(K.get_value(
        model.optimizer.lr)))
    K.set_value(model.optimizer.lr, 1e-5)
    print("[INFO] new learing rate: {}".format(K.get_value(
        model.optimizer.lr)))

# construct set of all callbacks
callbacks = [
    TrainingMonitor(config.FIG_PATH, config.JSON_PATH),
    EpochCheckpoint(args["checkpoint"], every=5, startAt=args["start_epoch"])
]

# training network
model.fit(trainGen.generator(),
          steps_per_epoch=(trainGen.numImages / 64),
          validation_data=valGen.generator(),
          validation_steps=(valGen.numImages / 64),
          ephochs=10,
          verbose=1,
          callbacks=callbacks)

trainGen.close()
valGen.close()
Exemple #15
0
    # update the learning rate
    print("[INFO] old learning rate: {}".format(
        K.get_value(model.optimizer.lr)))
    K.set_value(model.optimizer.lr, 1e-2)
    print("[INFO] new learning rate: {}".format(
        K.get_value(model.optimizer.lr)))
    print("[INFO] compiling model...")
# otherwise, load the checkpoint from disk
else:
    opt = SGD(lr=1e-1)
    model = ResNet.build(32, 32, 3, 10, (9, 9, 9),
                         (64, 64, 128, 256), regularization=0.0005)
    model.summary()
    print('[INFO] num of layers: {}'.format(len(model.layers)))
    model.compile(loss="categorical_crossentropy", optimizer=opt,
                  metrics=["accuracy"])
# construct the set of callbacks
callbacks = [
    EpochCheckpoint(CHECKPOINTS_DIR, every=5,
                    start_at=START_EPOCH),
    TrainingMonitor(fig_path="resnet56_cifar10.png",
                    json_path="resnet56_cifar10.json",
                    start_at=START_EPOCH)]
# train the network
print("[INFO] training network...")
model.fit_generator(
    aug.flow(x_train, y_train, batch_size=128),
    validation_data=(x_test, y_test),
    steps_per_epoch=len(x_train) // 128, epochs=10,
    callbacks=callbacks, verbose=1)