Exemple #1
0
def main():
	# Parse arguments.
	args = parse_args()
	
	# Set device.
	device = 'cuda' if torch.cuda.is_available() else 'cpu'

	# Load dataset.
	train_loader, test_loader, class_names = cifar10.load_data(args.data_dir)
	
	# Set a model.
	model = get_model(args.model_name)
	model = model.to(device)

	# Set loss function and optimization function.
	criterion = nn.CrossEntropyLoss()
	optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4)

	# Train and test.
	for epoch in range(args.n_epoch):
		# Train and test a model.
		train_acc, train_loss = train(model, device, train_loader, criterion, optimizer)
		test_acc, test_loss = test(model, device, test_loader, criterion)
		
		# Output score.
		stdout_temp = 'epoch: {:>3}, train acc: {:<8}, train loss: {:<8}, test acc: {:<8}, test loss: {:<8}'
		print(stdout_temp.format(epoch+1, train_acc, train_loss, test_acc, test_loss))

		# Save a model checkpoint.
		model_ckpt_path = args.model_ckpt_path_temp.format(args.dataset_name, args.model_name, epoch+1)
		torch.save(model.state_dict(), model_ckpt_path)
		print('Saved a model checkpoint at {}'.format(model_ckpt_path))
		print('')
Exemple #2
0
def main():
	# Parse arguments.
	args = parse_args()
	
	# Set device.
	device = 'cuda' if torch.cuda.is_available() else 'cpu'

	# Load dataset.
	train_loader, test_loader, class_names = cifar10.load_data(args.data_dir)
	
	# Load a model.
	model = get_model(args.model_name)
	model.load_state_dict(torch.load(args.model_path))
	model = model.to(device)
	print('Loaded a model from {}'.format(args.model_path))

	# Define loss function.
	criterion = nn.CrossEntropyLoss()
	
	# Test a model.
	test_acc, test_loss = test(model, device, test_loader, criterion)

	# Output score.
	stdout_temp = 'test acc: {:<8}, test loss: {:<8}'
	print(stdout_temp.format(test_acc, test_loss))
Exemple #3
0
def main():
    # Parse arguments.
    args = parse_args()

    # Set device.
    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    # Load dataset.
    train_loader, test_loader, class_names = cifar10.load_data(args.data_dir)

    # Set a model.
    model = get_model(args.model_name, args.n_feats)
    model = model.to(device)
    print(model)

    # Set a metric
    metric = metrics.ArcMarginProduct(args.n_feats,
                                      len(class_names),
                                      s=args.norm,
                                      m=args.margin,
                                      easy_margin=args.easy_margin)
    metric.to(device)

    # Set loss function and optimization function.
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD([{
        'params': model.parameters()
    }, {
        'params': metric.parameters()
    }],
                          lr=args.lr,
                          weight_decay=args.weight_decay)

    # Train and test.
    for epoch in range(args.n_epoch):
        # Train and test a model.
        train_acc, train_loss = train(device, train_loader, model, metric,
                                      criterion, optimizer)
        test_acc, test_loss = test(device, test_loader, model, metric,
                                   criterion)

        # Output score.
        stdout_temp = 'epoch: {:>3}, train acc: {:<8}, train loss: {:<8}, test acc: {:<8}, test loss: {:<8}'
        print(
            stdout_temp.format(epoch + 1, train_acc, train_loss, test_acc,
                               test_loss))

        # Save a model checkpoint.
        model_ckpt_path = args.model_ckpt_path_temp.format(
            args.dataset_name, args.model_name, epoch + 1)
        torch.save(model.state_dict(), model_ckpt_path)
        print('Saved a model checkpoint at {}'.format(model_ckpt_path))
        print('')
	print('Train autoencoder on cifar10 dataset')
	print('====================================')

	if len(sys.argv) >= 2:
		optimizer_name = sys.argv[1]
	print('Optimizer: ' + optimizer_name)
	if len(sys.argv) >= 3:
		learning_rate = float(sys.argv[2])
	print('Learning rate: ' + str(learning_rate))
	if len(sys.argv) >= 4:
		momentum = float(sys.argv[3])
	print('Momentum: ' + str(momentum))

	# load cifar10 dataset
	dataset_dir = '/home/jiawei/dataset/cifar-10/cifar-10-batches-py'
	train_set, test_set = cifar10.load_data(dataset_dir, False, False, True)
	X_train, Y_train = train_set
	X_test, Y_test = test_set
	print('Train set shape: ' + str(X_train.shape))
	print('Test set shape: ' + str(X_test.shape))

	with tf.device(gpu):
		# TF input placeholder
		X = tf.placeholder("float", [None, n_input])

		# Weights and biases
		#weights = {'encoder_h1': tf.Variable(tf.zeros([n_input, n_hidden_1], dtype=tf.float32)),
		#'encoder_h2': tf.Variable(tf.zeros([n_hidden_1, n_hidden_2], dtype=tf.float32)),
		#'decoder_h1': tf.Variable(tf.zeros([n_hidden_2, n_hidden_1], dtype=tf.float32)),
		#'decoder_h2': tf.Variable(tf.zeros([n_hidden_1, n_input], dtype=tf.float32)),
		#}
def load_dataset(dataset_name):
    if dataset_name == 'mnist':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
        )
    elif dataset_name == 'mnist-original':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
            original=True)
    elif dataset_name == 'mnist-rgb':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
            use_rgb=True)
    elif dataset_name == 'svhn':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = svhn.load_data(
        )
    elif dataset_name == 'svhn-extra':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = svhn.load_data(
            include_extra=True)
    elif dataset_name == 'mnist-svhn':
        anchor = load_dataset('mnist-rgb')
        mirror = load_dataset('svhn')
        dataset = CrossDomainDatasets(dataset_name.replace('-', ''), anchor,
                                      mirror)
    elif dataset_name == 'cifar10':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = cifar10.load_data(
        )
    elif dataset_name == 'moving-mnist':
        data = moving_mnist.load_data()
        dataset = TimeCorelatedDataset(dataset_name.replace('-', ''), data)
    elif dataset_name == 'lsun-bedroom':
        datapath = lsun.load_data()
        dataset = LargeDataset(datapath)
    elif dataset_name == 'celeba':
        datapath = celeba.load_data()
        dataset = LargeDataset(datapath)
    else:
        raise KeyError("Dataset not implemented")

    return dataset
Exemple #6
0
def load_dataset(dataset_name):
    if dataset_name == 'mnist':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
        )
    if dataset_name == 'stacked-mnist':
        dataset = StackedDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
        )
    elif dataset_name == 'mnist-original':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
            original=True)
    elif dataset_name == 'mnist-rgb':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = mnist.load_data(
            use_rgb=True)
    elif dataset_name == 'svhn':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = svhn.load_data(
        )
    elif dataset_name == 'svhn-extra':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = svhn.load_data(
            include_extra=True)
    elif dataset_name == 'mnist-svhn':
        anchor = load_dataset('mnist-rgb')
        mirror = load_dataset('svhn')
        dataset = CrossDomainDatasets(dataset_name.replace('-', ''), anchor,
                                      mirror)
    elif dataset_name == 'cifar10':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.x_test, dataset.y_test, dataset.attr_names = cifar10.load_data(
        )
    elif dataset_name == 'moving-mnist':
        data = moving_mnist.load_data()
        dataset = TimeCorelatedDataset(dataset_name.replace('-', ''), data)
    elif dataset_name == 'lsun-bedroom':
        datapath = lsun.load_data()
        dataset = LargeDataset(datapath)
    elif dataset_name == 'celeba':
        datapath = celeba.load_data()
        dataset = LargeDataset(datapath, buffer_size=20000)
    elif dataset_name == 'celeba-128':
        datapath = celeba.load_data(image_size=128)
        dataset = LargeDataset(datapath, buffer_size=5000)
    elif dataset_name == 'celeba-crop-128':
        datapath = celeba.load_data(image_size=128, center_crop=True)
        dataset = LargeDataset(datapath, buffer_size=5000)
    elif dataset_name == 'celeba-crop-64':
        datapath = celeba.load_data(image_size=64, center_crop=True)
        dataset = LargeDataset(datapath, buffer_size=20000)
    elif dataset_name == 'synthetic-8ring':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.attr_names = mixture.load_data(
            type="ring", n=8, std=.05, r=1, density=5000)
    elif dataset_name == 'synthetic-25grid':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.attr_names = mixture.load_data(
            type="grid", n=25, std=.05, density=2500)
    elif dataset_name == 'synthetic-high-dim':
        dataset = ConditionalDataset(name=dataset_name.replace('-', ''))
        dataset.images, dataset.attrs, dataset.attr_names = mixture.load_data(
            type="high-dim", n=10, d=1200, std=1., density=5000)
    elif dataset_name == 'mnist-anomaly':
        x, y, x_t, y_t, y_names = mnist.load_data()
        dataset = SimulatedAnomalyDetectionDataset(dataset_name.replace(
            '-', ''),
                                                   x,
                                                   y,
                                                   anomaly_class=0,
                                                   test_set=(x_t, y_t))
    elif dataset_name == 'svhn-anomaly':
        x, y, x_t, y_t, y_names = svhn.load_data()
        dataset = SimulatedAnomalyDetectionDataset(dataset_name.replace(
            '-', ''),
                                                   x,
                                                   y,
                                                   anomaly_class=0,
                                                   test_set=(x_t, y_t))
    elif dataset_name == 'cifar10-anomaly':
        x, y, x_t, y_t, y_names = cifar10.load_data()
        dataset = SimulatedAnomalyDetectionDataset(dataset_name.replace(
            '-', ''),
                                                   x,
                                                   y,
                                                   anomaly_class=0,
                                                   test_set=(x_t, y_t))
    else:
        raise KeyError("Dataset not implemented")

    return dataset
def main():
#load the model
    batch_size = 32
    model = keras.models.load_model('cifar10-1000-1.h5')
    print(model.summary())
    (x_train, y_train), (x_test, y_test) = cifar10.load_data()
    x_train = global_contrast_normalize(x_train)
    x_test = global_contrast_normalize(x_test)
    print(x_train.shape)
    x_train = x_train.reshape((len(x_train), 32, 32, 3))
    x_test = x_test.reshape((len(x_test), 32, 32, 3))
    print(x_test.shape)
    #print(x_test[1])
    nb_classes = 10
    nb_labels = 1000
    labels_per_class = nb_labels // nb_classes
    if nb_labels == 73257:
        labels_per_class = 1000000
    sample_inds = stratified_sample(y_test, labels_per_class)
    x_labeled = x_test[sample_inds]
    y_labeled = y_test[sample_inds]
    y_labeled = to_categorical(y_labeled)
    #print(x_labeled)
    #print(y_labeled)
    print(x_labeled.shape)
    print(y_labeled.shape)
    super_datagen = ImageDataGenerator(
            width_shift_range=3,
            height_shift_range=3,
            #horizontal_flip=hflip,
            preprocessing_function=gaussian_noise,
            fill_mode='reflect',
        )
    self_datagen = ImageDataGenerator(
            width_shift_range=3,
            height_shift_range=3,
            horizontal_flip=False,
            preprocessing_function=gaussian_noise,
            fill_mode='reflect',
        )

    super_data = super_datagen.flow(
            x_labeled,y_labeled,shuffle=True, batch_size=1, seed=None)
    self_data = self_datagen.flow(
            x_test, shuffle=True, batch_size=1, seed=None)
    #super_data = x_labeled
    #self_data = x_test
    train_data_loader = datagen(super_data, self_data, batch_size)
    print('self - data')
    print(self_data) 
    print('supervised - data')
    print(super_data)
    
    print('train_data_loader')
    print(train_data_loader)
    print(len(model.layers))
    print(model.layers)
    layer_name = 'convnet_trunk'
    layer_outputs = [model.get_layer(layer_name).get_output_at(2)]
    # extract the ouputs of the top 6 layers
    activation_model = Model(inputs=model.input,outputs=layer_outputs)
    steps = len(x_test)/batch_size
    activations = activation_model.predict_generator(train_data_loader,steps=steps,verbose=0)
    print(activations)
    print(activations.shape)
    k=6666
    first_layer_activation = activations[k]
    print(first_layer_activation.shape)
    print(first_layer_activation)
    #plt.imshow(first_layer_activation)
    #plt.show()
    #imsave('first_layer_activation.jpg',first_layer_activation)
    
    
    #plt.figure(1)
    plt.matshow(first_layer_activation[:,:,0])
    plt.show()
    #imsave('first_layer_activation'+str(k)+'[:,:,0].jpg',first_layer_activation[:,:,0])
    layer_name_2 = 'self_clf'
    layer_outputs_2 = [model.get_layer(layer_name_2).get_output_at(0)]
    representation_model = Model(inputs=model.input,outputs=layer_outputs_2)
    
    final_representation = np.array(representation_model.predict_generator(train_data_loader,steps = steps,verbose = 0))
    print('The shape of final_representation')
    print(final_representation.shape)
    #print(final_representation)
    from sklearn.preprocessing import StandardScaler
    scaler = StandardScaler()
    representation = scaler.fit_transform(final_representation)
    print(representation.shape)
    plot_representation = representation[:,0:2] #取其中的前两维
    print(plot_representation.shape)
    plot_Y = plot_representation
    plt.scatter(plot_Y[:, 0], plot_Y[:, 1], c = "green", marker='o', label='two')   
    #plt.show()
    plt.savefig('The_scatter_of_first_2_columns.jpg',dpi = None)
    '''kmeans = KMeans(n_clusters=10, init='k-means++')
    kmeans.fit(plot_Y)
    print(kmeans.inertia_)
    centroids = kmeans.cluster_centers_
    print(centroids)
    print(centroids.shape)'''
    #plt.scatter(centroids[:, 0], centroids[:, 1],
            #marker='x', s=169, linewidths=3,
            #color='w', zorder=10)
    #plt.savefig('centers-cifar10-sesemi-features-1.jpg')
    '''tsne = manifold.TSNE(n_components=2, init='pca', random_state=501)
    X_tsne = tsne.fit_transform(X)

    print("Org data dimension is {}.Embedded data dimension is {}".format(X.shape[-1], X_tsne.shape[-1]))
    x_min, x_max = X_tsne.min(0), X_tsne.max(0)
    X_norm = (X_tsne - x_min) / (x_max - x_min)  # 归一化
    plt.figure(figsize=(8, 8))
    for i in range(X_norm.shape[0]):
        plt.text(X_norm[i, 0], X_norm[i, 1], str(y[i]), color=plt.cm.Set1(y[i]), 
             fontdict={'weight': 'bold', 'size': 9})
        plt.xticks([])
        plt.yticks([])
        plt.show()
        plt.savefig('t-SNE-cifar10.jpg')'''
    '''superd = next(super_data)
    selfd = next(self_data)
    print('super - d')
    print(superd)
    print(superd.shape())
    print('self - d')
    print(selfd)    

    print('Try to extract the representation of the sesemi model')
    fig = plt.figure(figsize=(14,10))
    for n in range(1,29):
        fig.add_subplot(4, 7, n)
        img_tensor = [self_data[n],super_data[n]]
        #img_tensor = np.expand_dims(img_tensor, axis=0)
        #img_tensor /= 255.
        print('image tensor to be shown')
        print(img_tensor)
        print(len(img_tensor))
        #plt.imshow(self_data)
        #plt.show()
        #print(img_tensor2.shape)
        #img = expand_dims(img, axis=0)i
        #img = preprocess_input(img)
        img_tensor = list(itertools.chain.from_iterable(img_tensor))
        print(img_tensor.shape())
        img_tensor.flatten()
        print(img_tensor)
        feature_maps = model.predict(img_tensor)
        print(feature_maps)
        draw_features(feature_maps)
        plt.axis('off')
        plt.show()
    return
    print('Try to visualize the representation!')'''
    return
Exemple #8
0
        # inception (5a)
        outputs = self.inception(inputs=outputs, filters=filters)
        # inception (5b)
        filters = 1024
        outputs = self.inception(inputs=outputs, filters=filters)

        # classifier
        outputs0 = self.classifier_main(inputs=outputs, classes=1000)

        model = keras.Model(inputs=inputs,
                            outputs=[outputs0, outputs1, outputs2])

        model.summary()

        return model


if __name__ == '__main__':
    (X_train, Y_train), (X_test, Y_test) = cifar10.load_data()

    model = GoogLeNet().build(input_shape=(32, 32, 3), classes=10)

    model.compile(optimizer=keras.optimizers.SGD(lr=0.01,
                                                 momentum=0.9,
                                                 nesterov=True),
                  loss=keras.losses.categorical_crossentropy,
                  metrics=[
                      keras.metrics.categorical_accuracy,
                      keras.metrics.top_k_categorical_accuracy
                  ])
Exemple #9
0
def build_graph(dataset_path: str, log_dir: str, is_training: bool,
                epoch_num: int, batch_size: int,
                cnn_model: NerualNetwork.__class__, cnn_param: dict,
                sess: tf.Session):
    with sess.graph.as_default():
        global_step = tf.train.get_or_create_global_step(graph=sess.graph)

        image, label = load_data(dataset_path, is_training, epoch_num,
                                 batch_size)

        model = cnn_model()
        logits, loss = model.inference(image,
                                       10,
                                       label=label,
                                       param={
                                           **cnn_param,
                                           **{
                                               'global_steps': global_step
                                           }
                                       })
        tf.summary.scalar("loss", loss)

        learning_rate = tf.train.exponential_decay(0.001,
                                                   global_step,
                                                   10000,
                                                   0.9,
                                                   staircase=True)
        tf.summary.scalar("learning_rate", learning_rate)
        train_op = tf.train.AdamOptimizer(
            name='optimizer',
            learning_rate=learning_rate).minimize(loss,
                                                  global_step=global_step)

        summary_op = tf.summary.merge_all()

        saver = tf.train.Saver()

        # initialize variables
        latest_ckpt = tf.train.latest_checkpoint(os.path.expanduser(log_dir))
        if latest_ckpt is not None:
            # restore model
            saver.restore(sess, latest_ckpt)
        else:
            sess.run(tf.global_variables_initializer())

        # add accuracy node
        with tf.name_scope("accuracy"):
            if is_training:
                acc = tf.reduce_mean(
                    tf.cast(
                        tf.equal(tf.argmax(label, axis=1),
                                 tf.argmax(logits, axis=1)), tf.float32))
                acc_op = None
            else:
                acc, acc_op = tf.metrics.accuracy(tf.argmax(label, axis=1),
                                                  tf.argmax(logits, axis=1))
            tf.summary.scalar("acc", acc)
            acc_summary = tf.summary.merge_all()

        sess.run(tf.local_variables_initializer())

        # return train_op, acc, acc_op, loss, global_step, summary_op, saver
        return train_op, acc, acc_op, loss, global_step, summary_op, saver, acc_summary