def main():
    #  VGG16
    model = Vgg16Cifar10(imagenet_pre_trained=True)
    history = model.train()
    # plot the training loss and accuracy
    plot_diagnostics(history, model.name)
    # get the test data already loaded on initialization
    x_test = model.testX
    y_test = model.testY
    predicted_x = model.predict(x_test)
    residuals = np.argmax(predicted_x, 1) != np.argmax(y_test, 1)
    loss = sum(residuals) / len(residuals)
    print("the validation 0/1 loss is: ", loss)

    # load MNIST dataset for googlenet and resnet34
    trainX, trainY, valX, valY, testX, testY = load_dataset()

    #  GoogleNet
    img_dim = (28, 28, 1)
    model = GoogleNet.build_model(img_dim, 10)
    history = model.fit(trainX,
                        trainY,
                        epochs=10,
                        validation_data=(valX, valY),
                        batch_size=32,
                        verbose=2)
    _, acc = model.evaluate(testX, testY, verbose=2)
    print('Test accuracy: %.3f' % (acc * 100.0))
    # plot the training loss and accuracy
    plot_diagnostics(history, model.name)
    # make predictions on the test set
    preds = model.predict(testX)
    # show a nicely formatted classification report
    print(classification_report(testY.argmax(axis=1), preds.argmax(axis=1)))

    #  Resnet34
    model = Resnet34.build_model(img_dim, 10)
    history = model.fit(trainX,
                        trainY,
                        epochs=10,
                        validation_data=(valX, valY),
                        batch_size=128,
                        verbose=2)
    loss, acc = model.evaluate(testX, testY, verbose=2)
    print('Test accuracy: %.3f' % (acc * 100.0))
    # plot the training loss and accuracynip
    plot_diagnostics(history, model.name)
    # make predictions on the test set
    preds = model.predict(testX)
    # show a nicely formatted classification report
    print(classification_report(testY.argmax(axis=1), preds.argmax(axis=1)))
def load_model_with_data(model_name, input_data, num_classes, train=True):
    """

    Args:
        model_name:
        input_data:

    Returns:

    """
    if model_name == 'GoogleNet':
        return GoogleNet({'data':input_data}, num_classes=num_classes,
                                              trainable=train,
                                              name=model_name)
    if model_name == 'NIN':
        return NIN({'data':input_data}, num_classes=num_classes,
                                        name=model_name)
    raise ValueError('Model: {} is not supported'.format(model_name))
    pass
示例#3
0
    return Chot


def get_class_number(name):
    res = re.findall('[0-9]+', name)

    return int(res[0]) - 1


batch_size = 32

image = tf.placeholder(tf.float32, [None, 224, 224, 3])
image = tf.div(tf.subtract(image, tf.reduce_mean(image)), reduce_std(image))

labels = tf.placeholder(tf.float32, [None, 54])
net = GoogleNet({'data': image})

loss3_classifier_SOD = net.layers['loss3_classifier-SOD']

w = tf.Variable(tf.random_normal(shape=[100, 54],
                                 mean=0,
                                 stddev=2 / np.sqrt(100)),
                name='last_W')
b = tf.Variable(tf.zeros([54]), name='last_b')
output = tf.matmul(loss3_classifier_SOD, w) + b

cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=output))
tf.summary.scalar('Cost', cost)
opt = tf.train.AdamOptimizer(learning_rate=0.00001)
train_op = opt.minimize(cost)
示例#4
0
input_dir = args.data_dir
model_name = args.model_name
learning_rate = args.learning_rate
batch_size = args.batch_size
pt_iters = args.iterations
display_step = args.display_step
#################################################################################
#Manifest files
annotations = Annotations("data/streetstyle27ktrain.manifest")
attributes = annotations.get_attributes()[-12:]
categories = []
for attribute in attributes:
	categories.append(annotations.get_categories(attribute))
#################################################################################
# Init network
googlenet = GoogleNet(0.5, learning_rate, join('models', model_name), attributes, categories, image_dim[0])
################################################################################
if trainOrTest not in ["test", "train", "continue"]:
	print("argument can be one of 'test','train','continue'")
	exit()

if trainOrTest in ["train", "continue"]:
	logger = Logger("GoogleNet_finetuning_inmemory_"+model_name)
else:
	logger = Logger("GoogleNet_testing_inmemory_"+model_name)
################################################################################

aug_randgen = random.RandomState(42)

# A function to dynamically keep images in memory when loaded for first time
# Since there are just 27K images, can be done in-memory