Example #1
0
def gen_data_batch(source):
    data_gen = gen_data(source)
    while True:
        image_batch = []
        label_batch = []
        for _ in range(batch_size):
            image, label = next(data_gen)
            image_batch.append(image)
            label_batch.append(label)
        yield np.array(image_batch), np.array(label_batch)


images = tf.placeholder(tf.float32, [None, 28, 28, 1])
labels = tf.placeholder(tf.float32, [None, 10])
net = MyNet({'data': images})

ip2 = net.layers['ip2']
pred = net.layers['prob']

loss = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(logits=ip2, labels=labels), 0)
opt = tf.train.RMSPropOptimizer(0.001)
train_op = opt.minimize(loss)

with tf.Session() as sess:
    # Load the data
    sess.run(tf.global_variables_initializer())
    net.load('mynet.npy', sess)

    data_gen = gen_data_batch(mnist.train)
Example #2
0
    # split the train and test images
    train_file_path = imagePaths[:train_num]
    print('train num is', len(train_file_path))
    test_file_path = imagePaths[train_num:]
    print('test num is', len(test_file_path))
    # data ,label of train ,test
    trainX, trainY = load_data(train_file_path)
    testX, testY = load_data(test_file_path)
    # data augmentation
    aug = ImageDataGenerator(rotation_range=15, width_shift_range=0.1,
                             height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                             horizontal_flip=True, fill_mode="nearest")

    # initialize the model
    print("compiling model...")
    model = LeNet(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
    # model = AlexNet(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
    opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
    model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
    # model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    model.summary()
    plot_model(model, to_file='model.png')
    # train the network
    print("training network...")
    lr_decay = LearningRateScheduler(scheduler)
    H = model.fit_generator(aug.flow(trainX, trainY, batch_size=Batch_Size),
                            validation_data=(testX, testY), steps_per_epoch=len(trainX) // Batch_Size,
                            epochs=EPOCHS, verbose=1, callbacks=[lr_decay])

    # save the model
    print("save model...")
def gen_data_batch(source):
    data_gen = gen_data(source)
    while True:
        image_batch = []
        label_batch = []
        for _ in range(batch_size):
            image, label = next(data_gen)
            image_batch.append(image)
            label_batch.append(label)
        yield np.array(image_batch), np.array(label_batch)


images = tf.placeholder(tf.float32, [batch_size, 28, 28, 1])
labels = tf.placeholder(tf.float32, [batch_size, 10])
net = MyNet({'data': images})

ip2 = net.layers['ip2']
pred = tf.nn.softmax(ip2)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(ip2, labels), 0)
opt = tf.train.RMSPropOptimizer(0.001)
train_op = opt.minimize(loss)

with tf.Session() as sess:
    # Load the data
    sess.run(tf.initialize_all_variables())
    net.load('mynet.npy', sess)

    data_gen = gen_data_batch(mnist.train)
    for i in range(1000):