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)
    """                 
classWeight = classTotals.max() / classTotals
print(lb.classes_, classTotals, classWeight)

# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
#classification data
(trainX, testX, trainY, testY) = train_test_split(data,
                                                  labels,
                                                  test_size=0.20,
                                                  stratify=labels,
                                                  random_state=42)

# initialize the model
print("[INFO] compiling model...")
model = MiniVGGNet.build(height=height,
                         width=width,
                         depth=3,
                         classes=len(lb.classes_))
#number of epochs
num_epochs = 2
opt = SGD(lr=0.05, decay=0.01 / num_epochs, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

#save the best performing models
fname = args['output']
checkpoint = ModelCheckpoint(fname,
                             monitor="val_loss",
                             mode="min",
                             save_best_only=True,
                             save_weights_only=False,
aap = AspectAwarePreprocessor(64, 64)
iap = ImageToArrayPreprocessor()
print(iap.preprocess(imagePaths))
sdl = SimpleDatasetLoader(preprocessors=[aap, iap])
(data, labels) = sdl.load(imagePaths, verbose=500)
data = data.astype("float") / 255.0

(trainX, testX, trainY, test Y) = train_test_split(data, labels, 
test_size=0.25, random_state=42)

trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)

print("[INFO] compling model...")
opt = SGD(lr=0.05)
model = MiniVGGNet.build(width=64, height=64, depth=3,
classes=len(classNames))
model.compile(loss="categorical_crossentropy", optimizer=opt,
metrics=["accuracy"])
print("[INFO] training network...")
H = model.fit(trainX, trainY, validation_data=(testX, testY),
batch_size=32, epochs=100, verbose=1)
print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size=32)
print(classification_report(testY.argmax(axis=1),
preditions.argmax(axis=1), target_names=classNames))

# plot the training loss and accuracy
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
示例#4
0
def main():
    args = option()

    # construct the image generator for data augmentation
    aug = ImageDataGenerator(rotation_range=30,
                             width_shift_range=0.1,
                             height_shift_range=0.1,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             fill_mode="nearest")

    # grab the list of images
    print("[INFO] loading images...")
    imagePaths = list(paths.list_images(args["dataset"]))

    classNames = [pt.split(os.path.sep)[-2] for pt in imagePaths]
    classNames = [str(x) for x in np.unique(classNames)]

    # initialize the image preprocessors
    aap = AspectAwarePreprocessor(64, 64)
    iap = ImageToArrayPreprocessor()

    # load the dataset from disk
    sdl = SimpleDatasetLoader(preprocessors=[aap, iap])
    (data, labels) = sdl.load(imagePaths, verbose=500)
    # scale the raw pixel intensities to the range [0, 1]
    data = data.astype("float") / 255.0

    # split training: 75%, testing: 25%
    (trainX, testX, trainY, testY) = train_test_split(data,
                                                      labels,
                                                      test_size=0.25,
                                                      random_state=42)

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

    # initialize the optimizer and model
    print("[INFO] compiling model...")
    opt = SGD(lr=0.05)
    model = MiniVGGNet.build(width=64,
                             height=64,
                             depth=3,
                             classes=len(classNames))
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    # train the network
    print("[INFO] training network...")
    H = model.fit_generator(aug.flow(trainX, trainY, batch_size=32),
                            validation_data=(testX, testY),
                            steps_per_epoch=len(trainX) // 32,
                            epochs=100,
                            verbose=1)

    # evaluate the network
    print("[INFO] evaluating network...")
    predictions = model.predict(testX, batch_size=32)
    print(
        classification_report(testY.argmax(axis=1),
                              predictions.argmax(axis=1),
                              target_names=classNames))

    # plot the training loss and accuracy
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss")
    plt.plot(np.arange(0, 100), H.history["accuracy"], label="train_acc")
    plt.plot(np.arange(0, 100), H.history["val_accuracy"], label="val_acc")
    plt.title("Training Loss and Accuracy")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss/Accuracy")
    plt.legend()
    plt.savefig(args["output"])
testY = lb.transform(testY)

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

# define the set of callbacks to be passed to the model during
# training
callbacks = [LearningRateScheduler(step_decay)]

# initialize the optimizer and model
print('[INFO] compiling model...')
opt = SGD(lr=1e-2, 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'])

# train the network
print("[INFO] training the network...")
H = model.fit(trainX,
              trainY,
              validation_data=(testX, testY),
              batch_size=64,
              epochs=40,
              callbacks=callbacks,
              verbose=1)

# evaluate the network
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, decay=0.01 / 40, 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 callback to save only the *best* model to disk
    # base on validation loss
    fname = os.path.sep.join(
        [args["weights"], "weight-{epoch:03d}-{val_loss:.4f}.hdf5"])
    checkpoint = ModelCheckpoint(fname,
                                 monitor="val_loss",
                                 mode="min",
                                 save_best_only=True,
                                 verbose=1)
    callbacks = [checkpoint]

    # train the network
    print("[INFO] training network...")
    H = model.fit(trainX,
                  trainY,
                  validation_data=(testX, testY),
                  batch_size=64,
                  epochs=40,
                  callbacks=callbacks,
                  verbose=2)
def main():
    args = option()

    class_name = [
        'Apple', 'Avocado', 'Banana', 'Coconut', 'Custard_apple',
        'Dragon_fruit', 'Guava', 'Mango', 'Orange', 'Plum', 'Start_fruit',
        'Watermelon'
    ]

    in_data = 'H5PY/train/train_normal.h5'
    in_label = 'H5PY/train/labels_train.h5'

    # import the feature vector and trained labels
    h5f_data = h5py.File(in_data, 'r')
    h5f_label = h5py.File(in_label, 'r')

    data = h5f_data['dataset']
    labels = h5f_label['dataset']

    data = np.array(data)
    labels = np.array(labels)

    # reshape data matrix
    if K.image_data_format() == "channels_first":
        data = data.reshape(data.shape[0], 3, 32, 32)
    else:
        data = data.reshape(data.shape[0], 32, 32, 3)
    print(data.shape)

    # split training: 80%, testing: 20%
    (trainX, testX, trainY, testY) = train_test_split(data,
                                                      labels,
                                                      test_size=0.20,
                                                      random_state=42)

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

    # initialize the optimizer and model
    print("[INFO] compiling model...")
    opt = SGD(lr=0.05)
    model = MiniVGGNet.build(width=32, height=32, depth=3, classes=12)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    # train the network
    print("[INFO]training network ...")
    H = model.fit(trainX,
                  trainY,
                  validation_data=(testX, testY),
                  batch_size=32,
                  epochs=40,
                  verbose=1)

    # save the network to disk
    print("[INFO] serializing network ...")
    model.save(args["model"])

    # evaluate the network
    print("[INFO] evaluating network...")
    preds = model.predict(testX)
    print(
        classification_report(testY.argmax(axis=1),
                              preds.argmax(axis=1),
                              target_names=class_name))

    # plot the training loss and accuracy
    plt.style.use("ggplot")
    plt.figure()
    plt.plot(np.arange(0, 40), H.history["loss"], label="train_loss")
    plt.plot(np.arange(0, 40), H.history["val_loss"], label="val_loss")
    plt.plot(np.arange(0, 40), H.history["accuracy"], label="train_acc")
    plt.plot(np.arange(0, 40), H.history["val_accuracy"], label="val_acc")
    plt.title("Training Loss and Accuracy")
    plt.xlabel("Epoch #")
    plt.ylabel("Loss/Accuracy")
    plt.legend()
    plt.savefig(args["output"])