Example #1
0
def test_CNN_forward():
    frame = torch.zeros((3, 108, 60))
    layers_params = [(3, 32, 8, 4, 0, 1)]
    model = CNN(layers_params, None)
    assert model(frame).shape == (1, 32, 26, 14)
    layers_params = [(3, 32, 8, 4, 0, 1), (32, 64, 4, 2, 0, 1)]
    model = CNN(layers_params, None)
    assert model(frame).shape == (1, 64, 12, 6)
    return True
Example #2
0
    def __init__(self, dataset, size, NL_set, NF_set, lr_set, mom_set, indiv_list=[], \
                 train_loader=None, test_loader=None, train_batch_size=0, test_batch_size=0) :
        """
        \Description: Build a population of random individual
        \Args : 
            dataset             : dataset used
            size                : population's size (must be a multiple of 4)
            NL_set              : set of possible values for the number of hidden layers
            NF_set              : set of possible values for the number of feature maps
            lr_set              : set of possible values for the learning rate
            mom_set             : set of possible values for the momentum
            indiv_list          : list of individuals
            train_loader        : train loader
            test_loader         : test_loader
            train_batch_size    : size of the training batch
            test_batch_size     : size of the testing batch
        \Outputs : None
        """

        seed(654)  # Set the random seed

        self.dataset = dataset  # Dataset
        self.size = size  # Size of the population

        # Those are sets of values to choose from to create an indivudal
        self.NL_set = NL_set
        self.NF_set = NF_set
        self.lr_set = lr_set
        self.mom_set = mom_set

        self.pop = indiv_list[:]  # List of the individuals in the population
        self.train_loader = train_loader  # Train loader
        self.test_loader = test_loader  # Test loader
        self.train_batch_size = train_batch_size  # Size of the training batch
        self.test_batch_size = test_batch_size  # Size of the testing batch

        if indiv_list == []:
            # Create a random initial populations
            for i in range(0, self.size):

                # Append a random individual
                if torch.cuda.is_available():
                    self.pop.append(
                        CNN.CNN(dataset=self.dataset,
                                NL=choice(self.NL_set),
                                NF=choice(self.NF_set),
                                lr=choice(self.lr_set),
                                mom=choice(self.mom_set)).cuda())
                else:
                    self.pop.append(
                        CNN.CNN(dataset=self.dataset,
                                NL=choice(self.NL_set),
                                NF=choice(self.NF_set),
                                lr=choice(self.lr_set),
                                mom=choice(self.mom_set)))
def Ensemble_CNN(Data, Rs, Ns, M, full_list=False):
    """
    Ensemble Clustering using Common-Nearest-Neighbor algorithm.

    Parameters
    ----------
    Data : array
        Data x*y with x being the data points and y the features/dimensions.
    Rs : list of float
        Distance cut-offs R to be tested.
    Ns : list of int
        Neighbor cut-offs N to be tested.
    M : int
        Minimal number of data points within a cluster.
    full_list : bool, optional
        if True returns clustering results for every parameter set tested. The default is False.

    Returns
    -------
    coclustering : array
        Array containing the x*x coclustering matrix.
    """

    Tree = spatial.cKDTree(Data)
    coclustering = np.zeros((len(Data),len(Data)))
    norm = 0
    if full_list:
        Cluster_list_full={}
        for R in Rs:
            neighborlist, number_neighbors = get_neighborlist_tree(Tree, R)
            for N in Ns:
                nc = np.copy(number_neighbors)
                Cluster_list = CNN.CNN(Data, R, N, M, Ensemble=True, neighborlist=neighborlist, number_neighbors=nc)
                for cluster in Cluster_list:
                    x, y = np.meshgrid(cluster,cluster)
                    coclustering[x,y]+=1
                if Cluster_list:
                    norm +=1
                Cluster_list_full.update({str(R)+"_"+str(N):Cluster_list})
        return coclustering/norm, Cluster_list_full        
    else:
        for R in Rs:
            neighborlist, number_neighbors = get_neighborlist_tree(Tree, R)
            for N in Ns:
                nc = np.copy(number_neighbors)
                Cluster_list = CNN.CNN(Data, R, N, M, Ensemble=True, neighborlist=neighborlist, number_neighbors=nc)
                for cluster in Cluster_list:
                    x, y = np.meshgrid(cluster,cluster)
                    coclustering[x,y]+=1
                if Cluster_list:
                    norm +=1
        return coclustering/norm
def main(args):
    # Adagrad requires model to be moved to GPU before instantiating the optimizer
    model = CNN(height=96, width=96, channels=3).to(DEVICE)

    ### CHECKPOINT - load parameters, args, loss ###
    if args.resume_checkpoint != None and args.resume_checkpoint.exists():
        checkpoint = torch.load(args.resume_checkpoint)
        print(
            f"Resuming model {args.resume_checkpoint} that achieved {checkpoint['loss']} loss"
        )
        model.load_state_dict(checkpoint['model'])
        old_epochs = args.epochs
        args = checkpoint['args']
        args.epochs -= old_epochs

    train_loader = torch.utils.data.DataLoader(
        train_data,
        shuffle=True,
        batch_size=args.batch_size,
        pin_memory=True,
        num_workers=args.worker_count,
    )

    test_loader = torch.utils.data.DataLoader(
        test_data,
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )

    # criterion = lambda logits, labels : torch.mean(torch.sqrt(torch.sum(nn.MSELoss(reduction="none")(logits, labels), dim=1))).requires_grad_(True)
    criterion = nn.MSELoss()

    optimizer = optim.Adagrad(model.parameters(),
                              lr=args.learning_rate,
                              weight_decay=args.weight_decay)

    if args.lr_decay:
        optimizer = optim.Adagrad(model.parameters(),
                                  lr_decay=0.1,
                                  lr=args.learning_rate,
                                  weight_decay=args.weight_decay)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)

    trainer = Trainer(model, train_loader, test_loader, criterion, optimizer,
                      summary_writer, DEVICE)

    trainer.train(args.epochs,
                  args.val_frequency,
                  print_frequency=args.print_frequency,
                  log_frequency=args.log_frequency,
                  args=args)

    print("done training")

    summary_writer.close()
Example #5
0
def run_model(model_name):

    vocab_size, word_embeddings, train_iter, valid_iter, test_iter = load_data.load_dataset(
    )
    learning_rate = config.learning_rate
    batch_size = config.batch_size
    output_size = config.output_size
    hidden_size = config.hidden_size
    embedding_length = config.embedding_length

    epochs = config.epochs

    in_channels = config.in_channels
    out_channels = config.out_channels
    kernel_heights = config.kernel_heights
    stride = config.stride
    padding = config.padding
    keep_probab = config.keep_probab

    if model_name == 'CNN':
        model = CNN.CNN(batch_size, output_size, in_channels, out_channels,
                        kernel_heights, stride, padding, keep_probab,
                        vocab_size, embedding_length, word_embeddings)

    elif model_name == 'LSTM':
        model = LSTM_Attn.AttentionModel(batch_size, output_size, hidden_size,
                                         vocab_size, embedding_length,
                                         word_embeddings)

    loss_fn = F.cross_entropy
    path = "Saved Models/"
    for epoch in range(epochs):
        train_loss, train_acc = train_model(model, train_iter, epoch, loss_fn)
        val_loss, val_acc, y_test, y_pred = eval_model(model, valid_iter,
                                                       loss_fn)
        _, f, o = helper.getResult(y_test, y_pred)
        current_f1 = f['f1-score']
        checkpoint_model(model, path, current_f1, epoch + 1, model_name, 'max')
        print(
            f'Epoch: {epoch+1:02}, Train Loss: {train_loss:.3f}, Train Acc: {train_acc:.2f}%, Val. Loss: {val_loss:3f}, Val. Acc: {val_acc:.2f}%'
        )

    load_saved_model(model, path + '{}_best.pth'.format(model_name))
    test_loss, test_acc, y_test, y_pred = eval_model(model, test_iter, loss_fn)
    print(f'Test Loss: {test_loss:.3f}, Test Acc: {test_acc:.2f}%')

    print(
        "                                Overall               #               Fake                "
    )
    print(
        "                   precision    recall      f1-score  #  precision    recall      f1-score"
    )
    _, f, o = helper.getResult(y_test, y_pred)
    res = helper.printResult(model_name, o, f)
    print(res)
    path = model_name + "_results.txt"
    helper.saveResults(path, res)
 def trainModel(self):
     traningData, labels = self.loadTrainingData()
     cnn = CNN.CNN()
     cnn.build_model()
     cnn.train(traningData,
               labels,
               batch_size=128,
               nb_epoch=500,
               data_augmentation=False)
     cnn.save_model()
Example #7
0
 def on_evaluate_clicked(self):
     alert = QMessageBox()
     alert.setText('Evaluating Expression...please name the drawing!')
     # print(alert.exec_())
     visibleImage = self.drawingArea.image
     self.drawingArea.resizeImage(visibleImage, self.drawingArea.size())
     fileName = "CNN_input.JPG"
     visibleImage.save(fileName, "")
     neural_net = CNN.CNN()
     neural_net.predict(fileName)
def main(args):
    model = CNN(height=96, width=96, channels=3)

    ### CHECKPOINT - load parameters, args, loss ###
    if args.resume_checkpoint != None and args.resume_checkpoint.exists():
        checkpoint = torch.load(args.resume_checkpoint)
        print(
            f"Resuming model {args.resume_checkpoint} that achieved {checkpoint['loss']} loss"
        )
        model.load_state_dict(checkpoint['model'])
        old_epochs = args.epochs
        args = checkpoint['args']
        args.epochs -= old_epochs

    train_loader = torch.utils.data.DataLoader(
        train_data,
        shuffle=True,
        batch_size=args.batch_size,
        pin_memory=True,
        num_workers=args.worker_count,
    )

    test_loader = torch.utils.data.DataLoader(
        test_data,
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )

    criterion = nn.MSELoss()

    optimizer = optim.SGD(model.parameters(),
                          lr=args.learning_rate,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay,
                          nesterov=args.nesterov)

    log_dir = get_summary_writer_log_dir(args)
    print(f"Writing logs to {log_dir}")
    summary_writer = SummaryWriter(str(log_dir), flush_secs=5)

    trainer = Trainer(model, train_loader, test_loader, criterion, optimizer,
                      summary_writer, DEVICE)

    trainer.train(args.epochs,
                  args.val_frequency,
                  print_frequency=args.print_frequency,
                  log_frequency=args.log_frequency,
                  args=args)

    print("done training")

    summary_writer.close()
Example #9
0
def network(image, K, W, U, b, h, GRUStacks, M, N, subsetNum):

    # running the CNN
    imageConvRunData = CNN(image, K)

    # running the GRU
    for k in range(subsetNum):
        (h, GRUStacks) = GRU_Cell(M, N, h, imageConvRunData[0][5][k], W, U, b,
                                  GRUStacks)

    return (imageConvRunData, GRUStacks)
Example #10
0
def sort_cnn(image):
    a= transforms.Compose([transforms.Resize(32), transforms.CenterCrop(32),
                               transforms.ToTensor(), transforms.Normalize(mean=[0.4, 0.4, 0.4],
                                                                           std=[0.2, 0.2, 0.2])])
    pred_data = a(image)
    pred_data = pred_data.view(-1, 3, 32, 32)  # rebuild tensor
    cnn = CNN.CNN().to(device)
    cnn.load_state_dict(torch.load("data/model/trained.pkl"))  # Load model parameter weights
    sorted = cnn(pred_data.to(device))
    pred = sorted.max(1, keepdim=True)[1]
    return pred.item()
Example #11
0
def trainNetWithAllData():
    unsupervisedData, data, labels = createTrainingSet()

    print "data.shape"
    print data.shape
    print "labels.shape"
    print labels.shape

    data = common.scale(data)
    unsupervisedData = None

    activationFunction = activationfunctions.Rectified()
    rbmActivationFunctionVisible = activationfunctions.Identity()
    rbmActivationFunctionHidden = activationfunctions.RectifiedNoisy()

    unsupervisedLearningRate = 0.0001
    supervisedLearningRate = 0.001
    momentumMax = 0.99

    # net = db.DBN(4, [1200, 1500, 1000, len(args.emotions)],
    #            binary=False,
    #            activationFunction=activationFunction,
    #            rbmActivationFunctionVisible=rbmActivationFunctionVisible,
    #            rbmActivationFunctionHidden=rbmActivationFunctionHidden,
    #            unsupervisedLearningRate=unsupervisedLearningRate,
    #            supervisedLearningRate=supervisedLearningRate,
    #            momentumMax=momentumMax,
    #            nesterovMomentum=True,
    #            rbmNesterovMomentum=True,
    #            rmsprop=True,
    #            miniBatchSize=20,
    #            hiddenDropout=0.5,
    #            visibleDropout=0.8,
    #            momentumFactorForLearningRateRBM=False,
    #            firstRBMheuristic=False,
    #            rbmVisibleDropout=1.0,
    #            rbmHiddenDropout=1.0,
    #            preTrainEpochs=10,
    #            sparsityConstraintRbm=False,
    #            sparsityRegularizationRbm=0.001,
    #            sparsityTragetRbm=0.01)
    #
    # net.train(data, labels, maxEpochs=200,
    #           validation=False,
    #           unsupervisedData=unsupervisedData)

    net = cnn.CNN(width=30, height=40, classes=len(args.emotions))

    net.train(data, labels)

    with open(args.net_file, "wb") as f:
        pickle.dump(net, f)
    return net
Example #12
0
def test_CNN_transform():
    frame = np.zeros((3, 108, 60), dtype=np.uint8)
    layers_params = [(1, 1, 1, 1, 0, 1)]
    transform = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Grayscale(),
        transforms.Resize((54, 30)),
        transforms.ToTensor(),
        ])
    model = CNN(layers_params, transform)
    assert model(frame).shape == (1, 1, 54, 30)
    return True
Example #13
0
    def __init__(self,
                 input_shape,
                 action_space,
                 game_name,
                 memory=MAX_EXPERIENCES,
                 epsilon=1.0,
                 min_epsilon=MIN_EPSILON,
                 decay_rate=DECAY_RATE,
                 batch_size=BATCH_SIZE,
                 load_weights=True,
                 test=False):

        self.action_set = action_space  # if action_space <= 6 else 6
        self.input_shape = input_shape
        self.memory_size = memory
        self.epsilon = epsilon
        self.epsilon_min = min_epsilon
        self.decay = decay_rate
        self.batch_size = batch_size
        self.game = game_name
        print("Action Set: ", self.action_set)

        filepath = str("Assets/Weights/" + self.game +
                       "_weights") if load_weights else None

        self.policy_network = CNN(
            self.input_shape,
            self.action_set,
            batch_size=self.batch_size,
            weights=filepath if os.path.exists(filepath) else None)
        if not test:
            self.target_network = CNN(self.input_shape,
                                      self.action_set,
                                      batch_size=self.batch_size)

            self.target_network.model.set_weights(
                self.policy_network.model.get_weights())

            self.experiences = []
Example #14
0
def test_CNN_init():
    layers_params = [(3, 64, 3, 1, 0, 1)]
    model = CNN(layers_params, None)
    assert isinstance(model.layers[0], nn.Conv2d)
    assert isinstance(model.layers[1], nn.BatchNorm2d)
    assert isinstance(model.layers[2], nn.ReLU)
    assert model.layers[0].in_channels == 3
    assert model.layers[0].out_channels == 64
    assert model.layers[0].kernel_size == (3,3)
    assert model.layers[0].stride == (1,1)
    assert model.layers[0].padding == (0, 0)
    assert model.layers[0].dilation == (1, 1)

    return True
Example #15
0
 def _compose_model(self) -> Model:
     model = CNN(
         output_dim=self.output_dim,
         activation_fn=self.activation,
         stochastic_parameters=True,
         linear_model=True,
         dropout=self.dropout_rate > 0,
         dropout_rate=self.dropout_rate,
         uniform_init_minval=self.uniform_init_minval,
         uniform_init_maxval=self.uniform_init_maxval,
         w_init=self.w_init,
         b_init=self.b_init,
     )
     return model
Example #16
0
	def __init__(self,url,mode="TRAIN"):
		
		self.port = None
		self.mode = mode
		self.url = url

		if mode == "TRAIN":
			pygame.init()
			self.screen = pygame.display.set_mode((1280, 720))
			clock = pygame.time.Clock()
		if mode == "AUTO":
			self.net = CNN.CNN()
			self.net.load_model('trained_dataset.h5')

		self.training_file = None
def training():

    train_images, train_labels = input_data.get_files(TRAIN_DIR)
    batch_images, batch_labels = input_data.get_batches(
        train_images, train_labels, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits = CNN.CNN(batch_images, BATCH_SIZE, N_CLASSES)
    train_loss = Layers.loss(train_logits, batch_labels)
    train_op = Layers.optimize(train_loss, learning_rate)
    train_accuracy = Layers.accuracy(train_logits, batch_labels)

    summary_op = tf.summary.merge_all()

    sess = tf.Session()
    train_writer = tf.summary.FileWriter(LOGS_TRAIN_DIR, sess.graph)
    saver = tf.train.Saver()

    sess.run(tf.global_variables_initializer())

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    try:
        for step in range(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run(
                [train_op, train_loss, train_accuracy])

            if step % 50 == 0:
                print(
                    'Step %d, the training loss is %.2f, train accuracy is %.2f%%'
                    % (step, tra_loss, tra_acc))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 2000 == 0:
                checkpoint_path = os.path.join(LOGS_TRAIN_DIR, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Training Done.')

    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Example #18
0
def main(_):
    test_data, test_labels = load_data()
    test_size = len(test_data)
    # print(test_size)

    test_X = tf.placeholder(tf.float32,
                            shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE,
                                   NUM_CHANNELS))

    with tf.name_scope('cnn_model'):
        cnn = CNN.CNN(NUM_LABELS, EVAL_BATCH_SIZE, EVAL_BATCH_SIZE, IMAGE_SIZE,
                      NUM_CHANNELS, SEED)
    eval_prediction = tf.nn.softmax(cnn.model(test_X, drop_out=False))

    # Get all predictions for a dataset by running it in small batches.
    def eval_in_batches(data, sess):
        size = data.shape[0]
        if size < EVAL_BATCH_SIZE:
            raise ValueError("batch size for evals larger than dataset: %d" %
                             size)
        predictions = np.ndarray(shape=(size, NUM_LABELS), dtype=np.float32)
        for begin in xrange(0, size, EVAL_BATCH_SIZE):
            end = begin + EVAL_BATCH_SIZE
            if end <= size:
                predictions[begin:end, :] = sess.run(
                    eval_prediction, feed_dict={test_X: data[begin:end, ...]})
            else:
                batch_predictions = sess.run(
                    eval_prediction,
                    feed_dict={test_X: data[-EVAL_BATCH_SIZE:, ...]})
                predictions[begin:, :] = batch_predictions[begin - size:, :]
        return predictions

    # test process
    saver = tf.train.Saver()
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        # reload model
        ckpt = tf.train.latest_checkpoint(MODEL_PATH)
        saver.restore(sess, ckpt)

        predictions = eval_in_batches(test_data, sess)
        test_error = error_rate(predictions, test_labels)
        acc = 100.0 - test_error

        print('accuracy: %.1f%%' % acc)
Example #19
0
def restore_model(checkpoint_path, vocab_size):
    image_features_extract_model = CNN()
    encoder = CNN_Encoder(embedding_dim)
    decoder = RNN_Decoder(embedding_dim, units, vocab_size)
    optimizer = tf.keras.optimizers.Adam()

    ckpt = tf.train.Checkpoint(encoder=encoder,
                               decoder=decoder,
                               optimizer=optimizer)

    ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)
    # if a checkpoint exists, restore the latest checkpoint.
    if ckpt_manager.latest_checkpoint:
        ckpt.restore(ckpt_manager.latest_checkpoint)
        print('Latest checkpoint restored!')

    return image_features_extract_model, encoder, decoder
Example #20
0
    def __init__(self, snapshot_index=0):
        global class_index
        global predict_index
        class_index = int(sys.argv[2])
        predict_index = int(sys.argv[3])

        self.data = Data("../input/" + class_names[predict_index] +
                         "_test.list",
                         log="../log/test.data",
                         Test=True,
                         class_name=class_names[class_index])
        self.local_search_log = open("../log/local_search.log", "w")
        qnetwork = cPickle.load(
            open("../output/qnetwork/" + str(snapshot_index) + ".pkl", "r"))
        self.qnetwork = qnetwork
        self.qnetwork.reset_q()
        self.cnn = CNN("/mnt/caffenet.model", "/mnt/caffenet.deploy")
Example #21
0
    def __init__(self, metadata: Optional[MetaData] = None, *args, **kwargs) -> None:
        super().__init__()

        # Populate self.hparams with args and kwargs automagically!
        # We want to skip metadata since it is saved separately by the NNCheckpointIO object.
        # Be careful when modifying this instruction. If in doubt, don't do it :]
        self.save_hyperparameters(logger=False, ignore=("metadata",))

        self.metadata = metadata

        # example
        metric = torchmetrics.Accuracy()
        self.train_accuracy = metric.clone()
        self.val_accuracy = metric.clone()
        self.test_accuracy = metric.clone()

        self.model = CNN(num_classes=len(metadata.class_vocab))
Example #22
0
def init_CNN():
    
    # Initializes NN classifier
    clf = CNN(
        layer_sizes = [
                     {'type':'conv', 'f_H':3, 'f_W':3, 'n_C':10, 'stride':1, 'pad':0},
                     {'type':'pool', 'f_H':2, 'f_W':2, 'stride':2, 'mode':'max'},
                     {'type':'fc', 'size':20},
                     {'type':'fc', 'size':20}
                     ],
        learning_rate = 0.0005,
        max_iter = 75,
        L2 = 0,
        beta1 = 0.9, 
        beta2 = 0.999, 
        minibatch_size = 540,
        activation = 'relu',
        classification = 'multiclass',
        plot_N = 1,
        end_on_close = False,
        end_on_delete = True)
    
    return clf
Example #23
0
def prediction(ModelName, lines):

    string = ''
    wordList = []
    for image in lines:
        #if type(image) is str:  # case of ","
        if image == ',':
            wordList.append(string)
            string = ''
        else:  #calling model
            if ModelName == 'SVM':
                prediction = SVM.SVM(image)
            if ModelName == 'CNN':
                prediction = CNN.CNN(image)
            # if ModelName == 'KNN':
            #     prediction = SVM.SVM(image)
            #
            string += prediction
    return wordList


# word_list=prediction()
# print(word_list)
Example #24
0
from PIL import Image

import numpy as np
np.random.seed(10)  # Set the seed for reproductibility

PATCH_SIZE = 16  #Size of the patch
WINDOW_SIZE = 64  #Size of the context
ORIGINAL_SIZE = 400  #Size of the training images
BATCH_SIZE = 16  #Size of the batch
num_epoch = 40  #Number of epochs
learning_rate = 1e-4  #Initial learning rate
seed = 10  #Seed for reproductibility
pad_size = int((WINDOW_SIZE - PATCH_SIZE) / 2)  #Padding at each border

#Instantiate the CNN
New_CNN = CNN(num_epoch, learning_rate, PATCH_SIZE, WINDOW_SIZE, ORIGINAL_SIZE,
              BATCH_SIZE)

print("")
save_stuff = ""
while not (save_stuff is "1" or save_stuff is "2" or save_stuff is "3"
           or save_stuff is "4"):
    print("Choose one : ")
    print("Predict the Kaggle result [1]")
    print("Train the Kaggle Model, VGG16 + classifier [2]")
    print("Train VGG16 from scratch [3]")
    print("Train the custom CNN from scratch [4]")
    save_stuff = input("Choose one of [1,2,3,4]")

if save_stuff == "1":

    #Load the model
Example #25
0
from MultiLayerPerceptron import *
from CNN import *
import mnist_loader
import matplotlib.pyplot as plt
import numpy as np
training, validation, test = mnist_loader.load_data()
offset = 1000
t = training[0][0:offset], training[1][0:offset]
offsetv = 500
v = validation[0][0:offsetv], validation[1][0:offsetv]

#reshape x 28:28
#ndimage.convolve dla kazdego filtra i zapisac wynik w jakiejs liscie

a = CNN([980, 50, 10], None, 3, 1, 1)

a.train(t, v, 20, 0.001, 64, softplus_function, sigmoid_function, 20)
Example #26
0
def trainingCNN():
    # get the 32*32 pixel normalized picture with Center cut # Unify the pictures that need to be input to the model
    image_process = transforms.Compose([
        transforms.Resize(32),
        transforms.CenterCrop(32),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.4, 0.4, 0.4], std=[0.2, 0.2, 0.2])
    ])
    train_data = ImageFolder(root="data/train_set", transform=image_process)
    test_data = ImageFolder(root="data/test_set", transform=image_process)
    train_loader = DataLoader(train_data,
                              batch_size=10,
                              shuffle=True,
                              num_workers=1)
    test_loader = DataLoader(test_data,
                             batch_size=5,
                             shuffle=True,
                             num_workers=1)
    cnn = CNN.CNN().to(device)
    optimizer = torch.optim.Adam(cnn.parameters(),
                                 lr=0.001)  # Use Adam optimizer
    for epoch in range(3):  # edit epoch here
        for step, (img, label) in enumerate(train_loader):
            img, label = img.to(device), label.to(device)
            sort = cnn(img)
            loss = torch.nn.functional.nll_loss(
                sort,
                label)  # Use maximum likelihood / log likelihood cost function
            optimizer.zero_grad(
            )  # Pytorch will accumulate the gradient, so the gradient needs to be cleared # waste my a lot of time!
            loss.backward()
            optimizer.step()  # Use Adam for gradient update
            if (step + 1) % 3 == 0:
                print(
                    f'Train Epoch: {epoch + 1} [{step * len(img)}/{len(train_loader.dataset)} ({100. * step / len(train_loader):.0f}%)]\tLoss: {loss.item():.4f}'
                )

    cnn.eval()  # Check model accuracy
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for img, label in test_loader:
            img, label = img.to(device), label.to(device)
            sort = cnn(img)
            test_loss += torch.nn.functional.nll_loss(sort,
                                                      label,
                                                      reduction='sum').item()
            pred = sort.max(1, keepdim=True)[1]
            correct += pred.eq(label.view_as(pred)).sum().item()

        test_loss /= len(test_loader.dataset)
        print(
            f'\ntest loss={test_loss:.4f}, accuracy={float(correct) / len(test_loader.dataset):.4f}\n'
        )
    try:
        os.makedirs("data/model/")
    except OSError as e:
        if e.errno == 17:  # 17 means this file are already exists
            pass
        else:
            raise
    torch.save(cnn.state_dict(), "data/model/trained.pkl"
               )  # Save the model weights to the model directory
Example #27
0
import TFE as tfet
import mct_config as mctconfig
from MCT import *
import time
import random as rnd
import sys
import random
import CNN as cnn
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader,TensorDataset
import torch

# All the lovely neural network stuff goes here.
BOARD_WIDTH = 4
NN = cnn.CNN(BOARD_WIDTH * BOARD_WIDTH * 12)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(NN.parameters(), lr=0.01)

# Function to generate tuples of size two:
# (
#    UCB to next state taking direction i,
#    current node value
# )
# Input is specified NxN play field.
# UCB should be in dictionary form where
# keys are shown in DIR_KEY within mct_config

def generateValue(grid):
    NN.train(False)
    # Convert numpy grid to input for NN.
Example #28
0
#	* A convolutional layer contains N (count) x 3D convolutional kernels (FH, FW, C) which works on their 3D input activation (H, W, C).
#	* A fully connected layer contains a 2D (H, W) weight matrix (+ biases - count like output),
#	  where the W represents the flattened input activation map all pixels and H is the output vector elements.
layerTypeList = ["Convolution", "Convolution", "FullyConnected"]

# Describe the activation function types for each layer in a list. The list element can be "sigmoid", "linear", "relu", "softmax".
# NOTE: The "softmax" derivate function is not same with the literature. It is not reliable. Avoid to use it yet.
activationTypeList = ["relu", "sigmoid", "softmax"]

# Describe convolutional layer patterns [N, H, W, C], [count, height, width, channels] of kernels.
# Describe fully connected layer pattern [K, C], [all pixels of previous layer, output count] of kernels.
# WARNING: The Ni and Ci must be same between two consecutive convolutional layers.
weightSizeList = [[20, 5, 5, 1], [20, 3, 3, 20], [20 * 7 * 7, 10]]

# Initialization of a network.
cnn = CNN.CNN(initializationType, layerTypeList, activationTypeList,
              weightSizeList)
print("[" + __file__ + "]" + "[INFO]" + " CNN initialized.")

# TODO image preprocessing need to be modified and moved into a class/utility script.
print("[" + __file__ + "]" + "[INFO]" + " Preprocessing images.")
numberOfTrainExample = 200
xx = [None] * numberOfTrainExample
yy = [None] * numberOfTrainExample
for i in range(numberOfTrainExample):
    # xx is a list which contains selected images in 28x28 numpy array
    xx[i] = numpy.reshape(numpy.asarray(trainImages[i], dtype=numpy.float128),
                          (28, 28, 1))
    yy[i] = numpy.asarray(trainLabels[i])

print("[" + __file__ + "]" + "[INFO]" + " Start training.")
cnn.train(xx, yy, batchSize=1, numberOfEpochs=5, learningRate=0.0001)
def main(args):
    model = CNN(height=96, width=96, channels=3).to(DEVICE)

    ### CHECKPOINT - load parameters, args, loss ###
    if args.resume_checkpoint != None and args.resume_checkpoint.exists():
        if torch.cuda.is_available():
            checkpoint = torch.load(args.resume_checkpoint)
        else:
            # if CPU is used
            checkpoint = torch.load(args.resume_checkpoint, map_location=torch.device('cpu'))

        print(f"Testing model {args.resume_checkpoint} that achieved {checkpoint['loss']} loss")
        model.load_state_dict(checkpoint['model'])

    # visualise filters learnt by first conv layer
    weight = model.conv1.weight.data

    # normalise between 0 and 1
    min_val = torch.min(weight)
    max_val = torch.max(weight)
    norm_weight = (weight - min_val)/(max_val - min_val)

    fig, axes = plt.subplots(4,8)
    fig.suptitle("Filters of First Convolutional Layer")

    for i in range(4):
        for j in range(8):
            filter = norm_weight[8*i+j].cpu()
            axes[i, j].imshow(filter.permute(1,2,0))
            axes[i, j].axis('off')
            axes[i, j].set_title(8*i+j+1)

    plt.savefig(args.filter_path, dpi=fig.dpi)
    print(f"Filters of first conv layer saved to {args.filter_path}")

    test_loader = torch.utils.data.DataLoader(
        test_data,
        shuffle=False,
        batch_size=args.batch_size,
        num_workers=args.worker_count,
        pin_memory=True,
    )

    criterion = nn.MSELoss()

    preds = np.empty([0, 2304])
    total_loss = 0
    model.eval()

    # No need to track gradients for validation, we're not optimizing.
    with torch.no_grad():
        for batch, labels in test_loader:
            batch = batch.to(DEVICE)
            labels = labels.to(DEVICE)
            logits = model(batch)
            loss = criterion(logits, labels)
            total_loss += loss.item()
            preds = np.vstack((preds, logits.cpu().numpy()))

    average_loss = total_loss / len(test_loader)

    print(f"validation loss: {average_loss:.5f}")

    # Save predictions to preds.pkl and view it with visualisation/evaluation
    with open(args.preds_path, "wb") as f:
        pickle.dump(preds, f)

    print(f"Saved predictions to {args.preds_path}")
Example #30
0
# train the model

# shuffle data
randomOrder = np.random.permutation(len(segmentedData[:, 0, 0]))
labelRandom = labels[randomOrder]
segmentedDataRandom = segmentedData[randomOrder]

#f1 score over all validations
fScoreOverall = []
# split the dataset in five parts to train with 4 and test with 1 --> cross fold validation
samples = len(segmentedData[:, 0, 0])
sections = round(samples / len(listOfPatients))
for idx in range(len(listOfPatients)):

    # create new CNN
    cnn = CNN.CNN()
    cnn.create_model(len(listOfSensors))

    teststart = idx*sections
    testend = teststart + sections

    testData = segmentedDataRandom[teststart:testend, :, :]
    testLabel = labelRandom[teststart:testend]

    # trainData = segmentedDataRandom[0:idx*sections, :, :] + segmentedDataRandom[idx*sections+sections:, :, :]
    trainData = np.concatenate((segmentedDataRandom[0:idx*sections, :, :], segmentedDataRandom[idx*sections+sections:, :, :]))
    trainLabel = np.concatenate((labelRandom[0:idx*sections], labelRandom[idx*sections+sections:]))

    # from label to onehot Vector
    oneHotGT = DataHandler.getOneHotGTVectotr(trainLabel)