Esempio n. 1
0
def main(detectionalg=None):
    # Load Images
    img = pyplot.imread(imageDir)

    if detectionalg == 'ViolaJones':
        faces = ViolaJones.violaJones(img)

    elif detectionalg == 'CNN':
        faces = (face['box'] for face in CNN.detectCNN(img))
    else:
        raise IOError("Detection method does not specified.")

    # print bounding box for each detected face
    RectanglesCreator.draw_image(imageDir, faces)
Esempio n. 2
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
def testPoolingFunc():
    A = np.random.randint(300, size=(2, 5, 7))
    print("originale shape:", np.shape(A))
    print("first channel:", "\n", A[0])
    print("second channel:", "\n", A[1])

    kernelSize = (3, 2)
    stride = 2
    padding = (2, 1)
    poolingType = CNN.PoolingType.Max
    resultTensor = CNN.pooling(A, kernelSize, stride, padding, poolingType)
    print("new shape:", np.shape(resultTensor))
    print("first channel:", "\n", resultTensor[0])
    print("second channel:", "\n", resultTensor[1])
Esempio n. 4
0
def Train(x,
          y,
          model=None,
          epochs=100,
          batch_size=128,
          LR=0.001,
          n_layers=3,
          layer_size=128,
          filters=32,
          dropout=False,
          MaxPooling=False,
          Embedding=False,
          vocab_size=None,
          embedding_dim=64,
          loss="binary_crossentropy",
          train=False):

    if model == None:
        print("Please define the model: [CNN,DNN,RNN,GRU,LSTM,Transformer]")
    elif model == "CNN":
        Trained_Model = CNN.Train(x, y, epochs, batch_size, LR, n_layers,
                                  filters, dropout, MaxPooling, Embedding,
                                  vocab_size, embedding_dim, loss, train)
    elif model == "DNN":
        Trained_Model = DNN.Train(x, y, epochs, batch_size, LR, n_layers,
                                  layer_size, dropout, MaxPooling, Embedding,
                                  vocab_size, embedding_dim, loss, train)
    elif model == "RNN":
        Trained_Model = RNN.Train(x, y, epochs, batch_size, LR, n_layers,
                                  layer_size, dropout, MaxPooling, Embedding,
                                  vocab_size, embedding_dim, loss, train)
    elif model == "GRU":
        Trained_Model = GRU.Train(x, y, epochs, batch_size, LR, n_layers,
                                  layer_size, dropout, MaxPooling, Embedding,
                                  vocab_size, embedding_dim, loss, train)
    elif model == "LSTM":
        Trained_Model = LSTM.Train(x, y, epochs, batch_size, LR, n_layers,
                                   layer_size, dropout, MaxPooling, Embedding,
                                   vocab_size, embedding_dim, loss, train)
    elif model == "Transformer":
        Trained_Model = Transformer.Train(x,
                                          y,
                                          epochs,
                                          batch_size,
                                          LR,
                                          vocab_size,
                                          loss,
                                          embedding_dim,
                                          train=True)
    return Trained_Model
Esempio n. 5
0
def extensive_cnn(tt_dir, results_dir, params):
    y_train, y_test = tt.load_y(tt_dir)
    y_test_max = np.argmax(y_test, axis=1)

    X_train, X_test = tt.load_X_stacked(tt_dir, ud.features)

    for neurons in params['neurons']:
        for activation in params['activation']:
            for optimizer in params['optimizer']:
                for drop_out in params['drop_out']:
                    if neurons == 256 and drop_out != 0.5:
                        continue
                    for pooling in params['pooling']:
                        for epoch in params['epochs']:
                            for batch_size in params['batch_size']:
                                for feature_combi in params['features']:
                                    model_name = 'cnn_' \
                                        + str(neurons) + '_' \
                                        + activation + '_' \
                                        + optimizer + '_' \
                                        + str(drop_out) + '_' \
                                        + pooling + '_' \
                                        + str(epoch) + '_' \
                                        + str(batch_size) + '_' \
                                        + '_'.join(feature_combi)

                                    print('Training:', model_name)

                                    inputs = []
                                    for feat in feature_combi:
                                        inputs.append(ud.inputs_cnn[feat])

                                    #X_train, X_test = tt.load_X_stacked(tt_dir, feature_combi)

                                    model = CNN.get_cnn(inputs,
                                                        neurons=neurons,
                                                        activation=activation,
                                                        opt=optimizer,
                                                        drop_out=drop_out,
                                                        pooling=pooling)

                                    model.fit(X_train,
                                              y_train,
                                              epochs=epoch,
                                              batch_size=batch_size)

                                    predict_and_save_model(
                                        model, model_name, X_test, y_test_max,
                                        results_dir)
Esempio n. 6
0
 def dataset_path_updated(self, directory):
     distances_file_path = os.path.join(directory, DISTANCES_FILENAME)
     if os.path.isfile(distances_file_path):
         try:
             self.distances = list(CNN.get_pictures_distances(directory).items())
             self.set_captures_count(len(self.distances))
             self.captureNumberSpinBox.valueChanged.connect(self.set_current_capture)
             self.captureNumberSpinBox.setValue(1)
             self.captureNumberSpinBox.setMinimum(1)
             self.entryGroupBox.setEnabled(True)
         except OSError:
             self.disable_results()
             show_error(self, 'Impossible de charger le fichier de distances !')
     else:
         self.disable_results()
Esempio n. 7
0
def cnn():
    #app = Flask(__name__)
    #app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

    now = datetime.datetime.now()
    date = now.strftime('%Y-%m-%d')


    filename = './log/cnn.log'
    with open(filename,"a") as f:
        f.write(date)
        f.write("\n")
    print("날짜를 기록하였습니다")
    result=CNN.MoEs(date)
    return json.dumps(result, ensure_ascii=False)
def cnn_predict(company: str, verbose=False, train_size=0.80, scaled=False):
    X_train, X_test, y_train, y_test, prices, times = get_features(company, train_size=train_size, scaled=scaled)
    X_train, X_test = reshape(array(X_train), (shape(X_train)[0], shape(X_train)[1], 1)), reshape(array(X_test), (shape(X_test)[0], shape(X_test)[1], 1))
    y_train, y_test = reshape(array(y_train), (shape(y_train)[0], 1, 1)), reshape(array(y_test), (shape(y_test)[0], 1, 1))
    true_labels, CNN_predictions = CNN.predict(X_train, y_train, X_test, y_test)
    true_labels = reshape(true_labels, (shape(true_labels)[0], ))
    CNN_predictions = reshape(CNN_predictions, (shape(CNN_predictions)[0], ))
    accuracy = accuracy_score(true_labels, CNN_predictions)
    if verbose:
        print("Training Instances: " + str(len(X_train)))
        print("Testing Instances: " + str(len(X_test)))
        print()
        print("CNN Accuracy: " + str(accuracy * 100) + "%")
        prediction_distribution(CNN_predictions, true_labels)
    return prices, times, CNN_predictions, accuracy
Esempio n. 9
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
Esempio n. 10
0
def main():
    obs1 = CNN.CNNFeatureExtractor( ["images/%03d.png"%i for i in range(6)] )
    obs2 = srk.Observation( np.loadtxt("histogram_w.txt") )

    mlda1 = mlda.MLDA(3, [1000])
    mlda2 = mlda.MLDA(3, [50])
    mlda3 = mlda.MLDA(3, [50,50])
    
    mlda1.connect( obs1 )
    mlda2.connect( obs2 )
    mlda3.connect( mlda1, mlda2 )
    
    for it in range(5):
        mlda1.update()
        mlda2.update()
        mlda3.update()
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()
Esempio n. 12
0
    def __init__(self, X_train, Y_train, Net='LeNet5', opti='SGDMomentum'):
        # Prepare Data: Load, Shuffle, Normalization, Batching, Preprocessing
        self.X_train = X_train
        self.Y_train = Y_train

        self.batch_size = 64
        # D_in: input depth of network, 784, 28*28 input grayscale image
        self.D_in = 784
        # D_out: output depth of network = 10, the 10 digits
        self.D_out = 10

        print('  Net: ' + str(Net))
        print('  batch_size: ' + str(self.batch_size))
        print('  D_in: ' + str(self.D_in))
        print('  D_out: ' + str(self.D_out))
        print('  Optimizer: ' + opti)

        # =======================
        if Net == 'TwoLayerNet':
            # H is the size of the one hidden layer.
            H = 400
            self.model = ANN.TwoLayerNet(self.D_in, H, self.D_out)
        elif Net == 'ThreeLayerNet':
            #######################################
            ############  TODO   ##################
            #######################################
            # H1, H2 are the size of the two hidden layers.
            #self.model = ANN.ThreeLayerNet (self.D_in, H1, H2, self.D_out)
            print('Not Implemented.')
            exit(0)
        elif Net == 'LeNet5':
            self.model = CNN.LeNet5()

        # store training loss over iterations, for later visualization
        self.losses = []

        if opti == 'SGD':
            self.opti = optimizer.SGD(self.model.get_params(),
                                      lr=0.0001,
                                      reg=0)
        else:
            self.opti = optimizer.SGDMomentum(self.model.get_params(),
                                              lr=0.0001,
                                              momentum=0.80,
                                              reg=0.00003)

        self.criterion = loss.CrossEntropyLoss()
    def create(self):
        dataset = pd.read_csv(self.dataset_path, encoding='UTF-8')
        data_x , data_y = split_x_y(dataset,tokenize=True)
        
        # Spilt train : test = 0.8 : 0.2
        # x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, train_size = 0.8)
        # Full train (No Spliting)
        x_train = data_x
        y_train = data_y

        tokenizer = dummytokenize.dummytokenize()
        
        cnn = CNN.create_CNN(x_train,y_train,self.config,tokenizer=tokenizer.tokenize)

        cnn.fit_tfidf()
        cnn.build_CNN()
        cnn.compile()
Esempio n. 14
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")
Esempio n. 15
0
 def accept(self):
     import json
     try:
         with open(self.cnnPathLineEdit.text()) as cnn_json_file:
             from collections import OrderedDict
             ConvNet = CNN.convolutional_neural_network(self.heightSpinBox.value(),
                                                        self.widthSpinBox.value(),
                                                        self.channelsSpinBox.value(),
                                                        json.load(cnn_json_file, object_pairs_hook=OrderedDict))
         self.window = EvaluationWindow(ConvNet, self.cnnPathLineEdit.text(), self.checkpointPathLineEdit.text())
         self.window.move(self.app.desktop().availableGeometry().center() - self.window.rect().center())
         self.window.show()
         self.hide()
     except OSError as e:
         show_error(self, 'Impossible de charger le fichier du réseau !', e.strerror)
     except json.JSONDecodeError:
         show_error(self, 'Le fichier est invalide !')
Esempio n. 16
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)
Esempio n. 17
0
    def frames():
        camera = cv2.VideoCapture(Camera.video_source)
        if not camera.isOpened():
            raise RuntimeError('Could not start camera.')

        width = camera.get(3) #get width and height of img
        height = camera.get(4)
        
        crop_image_width = 224 #input size for pytorch pre-trained models
        crop_image_height = 224
        
        startW = int ((width-crop_image_width)/2)
        startH = int ((height - crop_image_height)/2)
        endH = startH + crop_image_height
        endW = startW + crop_image_width

        start_pt = (int(startW),int(startH))
        end_pt = (int(endW), int(endH) )
        color = (255,0,0)
        thickness = 1

        draw_H = startH - 20
        draw_W = int ((startW +endW)/2 )
        draw_pt = (int (draw_W), int(draw_H))
        font = cv2.FONT_HERSHEY_SIMPLEX


        while True:
            # read current frame
            _, img = camera.read()
            #DO ERROR CHECK IF WEBCAM CAMERA NOT DETECTED

            img = cv2.flip (img, 1)

            img = cv2.rectangle(img, start_pt, end_pt, color, thickness=thickness) #draw input rectangle

            cropped_img = img[startH:endH,startW:endW] #crop img to rectangle
            
            index = CNN.predict(model,cropped_img)

            prediction = Camera.letter_pred[index]

            img  = cv2.putText(img, prediction, draw_pt, font, 2, (0,0,255),3)

            # encode as a jpeg image and return it
            yield cv2.imencode('.jpg', img)[1].tobytes()
Esempio n. 18
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))
Esempio n. 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
def main():

    #Lets start by loading the Kaggle data
    X = np.load('../data/X_train.npy')
    y = np.load('../data/Y_train.npy')
    x_val = np.load('../data/X_validation.npy')
    y_val = np.load('../data/Y_validation.npy')

    model = CNN.buildModel(None, input_size)
    model.summary()

    train_dropout = model.fit(X,
                              y,
                              batch_size=batch_size,
                              epochs=epoch,
                              verbose=1,
                              validation_data=(x_val, y_val),
                              shuffle=True)

    test_eval = model.evaluate(x_val, y_val, verbose=1)
    print('Test loss:', test_eval[0])
    print('Test accuracy:', test_eval[1])
    print("Training finished")

    model.save_weights("CNN_model_weights.h5")

    accuracy = train_dropout.history['acc']
    val_accuracy = train_dropout.history['val_acc']

    loss = train_dropout.history['loss']
    val_loss = train_dropout.history['val_loss']
    epochs = range(len(accuracy))

    plt.plot(epochs, accuracy, 'bo', label='Training accuracy')
    plt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')
    plt.title('Training and validation accuracy')
    plt.legend()
    plt.figure()

    plt.plot(epochs, loss, 'bo', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.title('Training and validation loss')
    plt.legend()
    plt.show()
Esempio n. 21
0
   def __init__(self, network_type, index, complete_set, window, input_size):
      self.VARS = CNN_STATIC_VARIABLES.CNN_STATIC_VARS()
      subject_set = self.VARS.get_subject_set()

      if network_type == 'sd':
         convertion = self.VARS.CONVERTION_STATIC_DYNAMIC
         config = self.VARS.get_config(input_size, 2, index, 100, network_type)
         print 'Creating data set'
         self.data_set = input_data_window_large.read_data_sets(subject_set, self.VARS.len_convertion_list(convertion), convertion, None, window)
      
      if network_type == 'original':
         convertion = self.VARS.CONVERTION_ORIGINAL
         config = self.VARS.get_config(input_size, 17, index, 100, network_type)
         print 'Creating data set'
         self.data_set = input_data_window_large.read_data_sets(subject_set, self.VARS.len_convertion_list(convertion), convertion, None, window)
      
      if network_type == 'static':
         remove_activities = self.VARS.REMOVE_DYNAMIC_ACTIVITIES
         keep_activities = self.VARS.CONVERTION_STATIC
         config = self.VARS.get_config(input_size, 5, index, 100, network_type)
         self.data_set = input_data_window_large.read_data_sets_without_activity(subject_set, self.VARS.len_convertion_list(keep_activities), remove_activities, None, keep_activities, window)

      if network_type == 'dynamic':
         remove_activities = self.VARS.CONVERTION_STATIC
         keep_activities = self.VARS.CONVERTION_DYNAMIC
         config = self.VARS.get_config(input_size, 12, index, 100, network_type)
         self.data_set = input_data_window_large.read_data_sets_without_activity(subject_set, self.VARS.len_convertion_list(keep_activities), remove_activities, None, keep_activities, window)

      if network_type == 'shuf_stand':
         remove_activities = self.VARS.CONVERTION_SHUF_STAND_INVERSE
         keep_activities = self.VARS.CONVERTION_SHUF_STAND
         config = self.VARS.get_config(input_size, 3, index, 100, network_type)
         self.data_set = input_data_window_large.read_data_sets_without_activity(subject_set, len(keep_activities), remove_activities, None, keep_activities, window)


      self.cnn = CNN.CNN_TWO_LAYERS(config)
      self.cnn.set_data_set(self.data_set)

      self.cnn.load_model('models/' + network_type + '_' + str(input_size))
      if complete_set:
         print self.cnn.test_network()
      else:
         data = self.data_set.test.next_data_label(index)
         print np.argmax(data[1])+1, self.cnn.run_network(data)
Esempio n. 22
0
def build_CNN(CFG):
    # {{{
    """
    """
    if CFG['CNN_MODEL'] == "vgg":
        import CNN
        vgg16 = CNN.build_model()
        print "Loading pretrained VGG16 parameters"
        model_param_values = pickle.load(open('vgg16.pkl'))['param values']
        lasagne.layers.set_all_param_values(
            vgg16['prob'], model_param_values)
        l_input_img = vgg16['input']
        l_input_cnn = vgg16['fc7_dropout']
        #l_input_regions = vgg16['conv5_3']
        cnn = vgg16
    elif CFG["CNN_MODEL"] == "resnet":
        import resnet_CNN
        resnet50 = resnet_CNN.build_model(
            input_layer=None, batch_size=CFG['BATCH_SIZE'])
        model_param_values = pickle.load(open('resnet50.pkl'))[
            'param values']

        from save_layers import add_names_layers_and_params, set_param_dict
        add_names_layers_and_params(resnet50)
        #lasagne.layers.set_all_param_values(resnet50['prob'], model_param_values)
        set_param_dict(resnet50['prob'], model_param_values,
                       prefix='', show_layers=False, relax=False)
        l_input_img = resnet50['input']

        if CFG['RESNET_LAYER'] == 'prob':
            # ExpressionLayer(vgg16['fc7_dropout'],lambda X:X[:,0,:], output_shape='auto')#the first bbox is the entire image
            l_input_cnn = resnet50['prob']
        elif CFG['RESNET_LAYER'] == 'pool5':
            l_input_cnn = resnet50['pool5']
        else:
            print("Layer not supported for resnet")
            raise ValueError()
            #print(bcolors.FAIL + "Resnet not yet handled" + bcolors.ENDC)
        cnn = resnet50
    else:
        print(bcolors.FAIL + "Unknown CNN selected" + bcolors.ENDC)

    return cnn, l_input_img, l_input_cnn
Esempio n. 23
0
def train_test_3DCNN(data, resize):
    x, y = data_transform_for_RCNN(data, resize)
    kv = BCI.gen_kv_idx(y, 5)
    acc = []
    for train_idx, test_idx in kv:
        x_train, y_train = x[train_idx], y[train_idx]
        x_test, y_test = x[test_idx], y[test_idx]
        model = CNN.create_3d_model()
        model.fit(x_train,
                  y_train,
                  validation_data=(x_test, y_test),
                  epochs=10)
        metrics = model.evaluate(x_test, y_test)
        for i in range(len(model.metrics_names)):
            if (str(model.metrics_names[i] == 'acc')):
                acc.append(metrics[i])
    pen = open('/result.csv', 'a')
    acc_sen += [str(v) for v in acc]
    pen.write()
def test(test_set, PATH):

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

    classes = ('0', '1')

    batch_size = 4
    num_workers = 2

    test_loader = torch.utils.data.DataLoader(test_set,
                                              shuffle=False,
                                              batch_size=batch_size,
                                              num_workers=num_workers)

    net = CNN.Net().to(device)
    net.load_state_dict(torch.load(PATH))

    correct = 0
    total = 0
    n_class_correct = [0 for i in range(2)]
    n_class_samples = [0 for i in range(2)]
    with torch.no_grad():
        for (images, labels) in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = net(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

            for i in range(len(images)):
                label = labels[i]
                pred = predicted[i]
                if label == pred:
                    n_class_correct[label] += 1
                n_class_samples[label] += 1

    for i in range(2):
        acc = 100 * n_class_correct[i] / n_class_samples[i]
        print('Accuracy of %s: %.3f %%' % (classes[i], acc))

    print('Accuracy of the network on the test images: %.3f %%' %
          (100 * correct / total))
 def run():
     global model, entrenamiento
     while True:
         try:
             model, entrenamiento = CNN.cnn(ventana_convolucion,ventana_display,X_train,Y_train,X_test,Y_test,
                      lote,optimizador,prop_val,numero_capas_convolucionales,numero_capas_fullcon,loss,
                      parada,iteraciones,control,delta,paciencia, NF,TF,RE,PAS,PO,TAMPOL,PASPOL,ACON,DRC,NO,AC,BA,DR)
             break
         except tk.TclError:
             mb.showerror("Error desconocido", "Por favor vuelva a intentarlo ")
             ventana_display.destroy()
             return
         except RuntimeError:
             mb.showerror("Error desconocido", "Por favor reinicie la aplicación ")
             ventana_display.destroy()
             return
         except sklearn.metrics.classification.UndefinedMetricWarning:
             mb.showerror("Error ", "Algo salió mal con los datos, reinicie la aplicación y vuelva a intentarlo ")
             ventana_display.destroy()
             return                 
Esempio n. 26
0
def targetVectorConvertTest(targets):
    modelTest = CNN.SequentialModel()
    modelTest.AddTargetVectors(targets)
    checkArray = modelTest.GetTargetVectors()

    for z in range(targets.shape[2]):
        for row_i in range(targets.shape[0]):
            for col_j in range(targets.shape[1]):
                #print("z = ", z, "i = ", row_i, "j = ", col_j)
                if checkArray[z][row_i][col_j] != targets[row_i, col_j, z]:
                    print(
                        "ERROR: Input image arrays do not match converted vector arrays in SequentialModel for i = ",
                        row_i, " j = ", col_j, " z = ", z)
                    print("Vector element value = ",
                          checkArray[z][row_i][col_j])
                    print("Input image array value = ", targets[row_i, col_j,
                                                                z])
                    return False

    return True
Esempio n. 27
0
def imageArrayConvertTest(inputImageArray):
    modelTest = CNN.SequentialModel()
    modelTest.AddInputDataPoints(inputImageArray)
    checkArray = modelTest.GetInputDataPointsVector()

    for z in range(inputImageArray.shape[2]):
        for row_i in range(inputImageArray.shape[0]):
            for col_j in range(inputImageArray.shape[1]):
                #print("z = ", z, "i = ", row_i, "j = ", col_j)
                if checkArray[z][row_i][col_j] != inputImageArray[row_i, col_j,
                                                                  z]:
                    print(
                        "ERROR: Input image arrays do not match converted vector arrays in SequentialModel for i = ",
                        row_i, " j = ", col_j, " z = ", z)
                    print("Vector element value = ",
                          checkArray[z][row_i][col_j])
                    print("Input image array value = ",
                          inputImageArray[row_i, col_j, z])
                    return False

    return True
Esempio n. 28
0
def run_with_cnn(image_filename="../Wheat_Images/004.jpg", ser_filename=None):
    '''
	Estimates the number of grains in a given image using a
	Convolutional neural network.

	Args:
		image_filename: The path to the image from which a grain count
			is to be obtained.

		ser_filename: path to serialized list of isub-images already extracted
		from the image from which a grain count is to be obtained.

	Returns:
		count: An estimate of the number of grains in the provided image.
	'''

    global img_data

    # Chop image up into sub-images and serilaise or just load serialised data if
    # it already exists.
    if (ser_filename == None and image_filename == "../Wheat_Images/004.jpg"):
        ser_filename = "../Wheat_Images/xxx_004.data"
    if (Helper.unserialize(ser_filename) == None):
        img = img_as_ubyte(io.imread(image_filename))
        roi_img = spectral_roi.extract_roi(img, [1])
        Helper.block_proc(roi_img, (20, 20), blockfunc)
        #Helper.serialize(ser_filename, img_data)
    else:
        img_data = Helper.unserialize(ser_filename)

    # classify
    r = CNN.classify(img_data[0],
                     model_file=None,
                     featureRepresentation='glcm',
                     shouldSaveResult=True)

    # Count number of '1s' in the result and return
    count = r.tolist().count(1)
    print("COUNT: {}".format(count))
    return count
Esempio n. 29
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)
Esempio n. 30
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
Esempio n. 31
0
	def run_tests(self):
		count=0
		for vocabSize in self.vocabSizeVector:
			for maxSequenceLength in self.maxSequenceLengthVector:
				for vectorizationType in self.vectorizationTypeVector:
					for epochs in self.epochsVector:
						for minNumArticlesPerDewey in self.minNumArticlesPerDeweyVector:
							new_configFile, run_name = self.create_config_file(vocabSize, maxSequenceLength,
							                                                   vectorizationType, epochs,
							                                                   minNumArticlesPerDewey)
							tid = time.time()
							count += 1
							run_length = len(self.vocabSizeVector) * len(self.maxSequenceLengthVector) * len(
								self.vectorizationTypeVector) * len(self.epochsVector) * len(
								self.minNumArticlesPerDeweyVector)
							print("Gjør test nr {} av {} : ".format(count, run_length))
							cnn_model = CNN.cnn(new_configFile)
							cnn_model.fit()
							cnn_model.predict(self.testSetPath)
							cnn_model.run_evaluation()
							new_logPath = os.path.join(self.logFolder, run_name + ".log")
							cnn_model.printResultToLog(new_logPath)
							print("Det tok {} \n".format(time.time() - tid))
Esempio n. 32
0
def run_with_cnn(image_filename="../Wheat_Images/004.jpg", ser_filename=None):
    '''
	Estimates the number of grains in a given image using a
	Convolutional neural network.

	Args:
		image_filename: The path to the image from which a grain count
			is to be obtained.

		ser_filename: path to serialized list of isub-images already extracted
		from the image from which a grain count is to be obtained.

	Returns:
		count: An estimate of the number of grains in the provided image.
	'''

    global img_data

    # Chop image up into sub-images and serilaise or just load serialised data if
    # it already exists.
    if(ser_filename == None and image_filename == "../Wheat_Images/004.jpg"):
		ser_filename = "../Wheat_Images/xxx_004.data"
    if(Helper.unserialize(ser_filename) == None):
        img = img_as_ubyte(io.imread(image_filename))
        roi_img = spectral_roi.extract_roi(img, [1])
        Helper.block_proc(roi_img, (20,20), blockfunc)
        #Helper.serialize(ser_filename, img_data)
    else:
        img_data = Helper.unserialize(ser_filename)

    # classify
    r = CNN.classify(img_data[0], model_file=None,featureRepresentation='glcm', shouldSaveResult=True)

    # Count number of '1s' in the result and return
    count = r.tolist().count(1)
    print("COUNT: {}".format(count))
    return count
Esempio n. 33
0
def learn(inputs, error):    
    fcnn.backprop(net.receptors, net.synapses, net.bias, net.deltas, error) 
    cnn.backprop(net.filters, net.conv_bias, net.pooled, net.switches, net.conv_error, net.conv_delta, net.deltas[0], inputs)
Esempio n. 34
0
def execute_net(inputs):
    cnn.fwdPass(inputs, net.convolved, net.filters, net.conv_bias, net.pooled, net.switches)        
    fcnn.fwdPass(net.pooled[net.conv_layers-1], net.receptors, net.synapses, net.bias)    #pass the CNN output to FC NNet
Esempio n. 35
0
#################################---CREATE MODEL---########################################
x,y = training[0][0].shape
inputShape = (1,x,y) # images are 1 channel

# Create layers to be used
# dict = {'Name': 'Zara', 'Age': 7} # keys are Name and Age, values are Zara and 7
layers = [
        {'Convolution': {'filterSize': 5, 'stride': 1, 'numFilters': 20} }, # convolution layer
        {'Pooling': {'poolSize': (2,2)} }, # pooling to reduce computation
        {'fullyConnected': {'numOutput': 50} }, # full connected layer at the end
        {'outputLayer': {'numClasses': 10} } # 10 classes for digits 0-9
        ]

# Create Model
test = CNN(inputShape,layers) # init model
print(' ')
print('Model Initialized')

##################################---TRAIN MODEL---#########################################
batchSize = 10 # number of training images per batch, weights updated after each batch
if smax: learningRate = .1 # Best learning rate found for softmax (from testing)
else: learningRate = 1.5 # Best learning rate foudn for mse (from testing)
numEpochs = 5 # num of epochs to loop through
test.train(training,batchSize,learningRate,numEpochs) # train the model


#################################---GET ACCURACY---############################################
def getAccuracy(net,testData):
    print('Begin Testing')
    numCorrect = 0
Esempio n. 36
0
class TestSystem:
    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")

    def local_search_element(self, element, cur_size):
        local_boxes = local_search(element.box, cur_size)
        res_element = [element]
        for box in local_boxes:
            loop = True
            self.data.crop_box = box
            t = 5
            prob = 0.0
            overlap_ratio = 0.0
            tmp_element = -1
            while (t != 0) and loop:
                state = self.data.get_state()
                action = self.qnetwork.choose_action_test(state)
                overlap_ratio = get_overlapping_ratio(self.data.crop_box, self.data.cur_box)
                new_element = Element(self.data.crop_box, class_index, 0, overlap_ratio, -1)
                crop_img = self.data.next_crop(action, False)
                if (not self.data.not_outside()) or (action == 16):
                    loop = False
                t = t - 1
                tmp_element = new_element
            res_element.append(tmp_element)
        size = len(res_element)
        co_overlap_mat = np.zeros(size * size, dtype=float).reshape((size, size))
        for i in range(size):
            for j in range(i, size):
                co_overlap_mat[i][j] = get_overlapping_ratio(res_element[i].box, res_element[j].box) / 10.0
                co_overlap_mat[j][i] = co_overlap_mat[i][j]
        for i in range(size):
            res_element[i].local_search_index = co_overlap_mat[i].sum()
        sorted_list = res_element
        for ele in sorted_list:
            self.local_search_log.write(str(ele) + "\n")
        self.local_search_log.write("\n")
        if sorted_list[0].local_search_index > local_search_index_threshold:
            return sorted_list[0]
        else:
            return -1

    def gen_initial_boxes(self, cur_size):
        res = []
        res.append([0, 0, cur_size[0], cur_size[1]])
        res.append([0, 0, int(0.5 * cur_size[0]), int(0.5 * cur_size[1])])
        res.append([int(0.5 * cur_size[0]), 0, int(cur_size[0]), int(0.5 * cur_size[1])])
        res.append([0, int(0.5 * cur_size[1]), int(0.5 * cur_size[0]), int(cur_size[1])])
        res.append([int(0.5 * cur_size[0]), int(0.5 * cur_size[1]), int(cur_size[0]), int(cur_size[1])])
        res.append([int(0.00 * cur_size[0]), int(0.00 * cur_size[1]), int(0.25 * cur_size[0]), int(0.25 * cur_size[1])])
        res.append([int(0.25 * cur_size[0]), int(0.00 * cur_size[1]), int(0.50 * cur_size[0]), int(0.25 * cur_size[1])])
        res.append([int(0.50 * cur_size[0]), int(0.00 * cur_size[1]), int(0.75 * cur_size[0]), int(0.25 * cur_size[1])])
        res.append([int(0.75 * cur_size[0]), int(0.00 * cur_size[1]), int(1.00 * cur_size[0]), int(0.25 * cur_size[1])])
        res.append([int(0.00 * cur_size[0]), int(0.25 * cur_size[1]), int(0.25 * cur_size[0]), int(0.50 * cur_size[1])])
        res.append([int(0.25 * cur_size[0]), int(0.25 * cur_size[1]), int(0.50 * cur_size[0]), int(0.50 * cur_size[1])])
        res.append([int(0.50 * cur_size[0]), int(0.25 * cur_size[1]), int(0.75 * cur_size[0]), int(0.50 * cur_size[1])])
        res.append([int(0.75 * cur_size[0]), int(0.25 * cur_size[1]), int(1.00 * cur_size[0]), int(0.50 * cur_size[1])])
        res.append([int(0.00 * cur_size[0]), int(0.50 * cur_size[1]), int(0.25 * cur_size[0]), int(0.75 * cur_size[1])])
        res.append([int(0.25 * cur_size[0]), int(0.50 * cur_size[1]), int(0.50 * cur_size[0]), int(0.75 * cur_size[1])])
        res.append([int(0.50 * cur_size[0]), int(0.50 * cur_size[1]), int(0.75 * cur_size[0]), int(0.75 * cur_size[1])])
        res.append([int(0.75 * cur_size[0]), int(0.50 * cur_size[1]), int(1.00 * cur_size[0]), int(0.75 * cur_size[1])])
        res.append([int(0.00 * cur_size[0]), int(0.75 * cur_size[1]), int(0.25 * cur_size[0]), int(1.00 * cur_size[1])])
        res.append([int(0.25 * cur_size[0]), int(0.75 * cur_size[1]), int(0.50 * cur_size[0]), int(1.00 * cur_size[1])])
        res.append([int(0.50 * cur_size[0]), int(0.75 * cur_size[1]), int(0.75 * cur_size[0]), int(1.00 * cur_size[1])])
        res.append([int(0.75 * cur_size[0]), int(0.75 * cur_size[1]), int(1.00 * cur_size[0]), int(1.00 * cur_size[1])])
        return res

    def test_xml_image(self):
        fout = []
        for i in range(1, predict_index + 1):
            fout.append(open("../output/test/comp3_det_test_" + class_names[i - 1] + ".txt", "r"))
        fout.append(open("../output/test/comp3_det_test_" + class_names[predict_index] + ".txt", "w"))
        count = 0
        index = 0
        upper_count = 0
        while (not self.data.next_image()) and index != total:
            print self.data.xml_image
            imgid = self.data.xml_image.split("ns/")[1].split(".xml")[0]
            output_list = []
            upper_counted = False
            initial_boxes = self.gen_initial_boxes(self.data.cur_size)
            k = 0
            while len(initial_boxes) != 0:
                print "%dth crop" % (k)
                k = k + 1
                past_list = []
                loop = True
                self.data.crop_box = initial_boxes.pop()
                t = num_iter
                prob = 0.0
                overlap_ratio = 0.0
                tmp_element = -1
                while (t != 0) and loop:
                    state = self.data.get_state()
                    action = self.qnetwork.choose_action_test(state)
                    overlap_ratio = get_overlapping_ratio(self.data.crop_box, self.data.cur_box)
                    if overlap_ratio > 5 and (not upper_counted):
                        upper_count = upper_count + 1
                        upper_counted = True
                    new_element = Element(
                        transform_box(self.data.crop_box, self.data.cur_size),
                        predict_index,
                        -1,
                        overlap_ratio,
                        self.qnetwork.max_q_value,
                    )
                    print "%f, %s, %f" % (overlap_ratio, str(self.data.crop_box), self.qnetwork.max_q_value)
                    self.data.next_crop(action, False)

                    if (not self.data.not_outside()) or (action == 16):
                        loop = False
                    if NMS(past_list, self.data.crop_box):
                        loop = False
                    else:
                        past_list.append(self.data.crop_box)
                    if tmp_element == -1 or tmp_element.q_val > new_element.q_val:
                        tmp_element = new_element
                    t = t - 1
                if tmp_element != -1:
                    self.data.save_crop_image(tmp_element.box, "../log/test.jpg")
                    class_predict, proba = self.cnn.get_class("../log/test.jpg")
                    print "%d:%f" % (class_predict, proba)
                    if class_predict == class_index + 1:
                        tmp_element.local_search_index = proba
                        print "end: %f, %s, %f" % (
                            tmp_element.overlap_ratio,
                            str(tmp_element.box),
                            tmp_element.local_search_index,
                        )
                        output_list.append(tmp_element)
                    else:
                        print "-1"
                else:
                    print "-1"
            sorted_list = sorted(output_list, key=lambda element: element.local_search_index, reverse=True)

            res = False
            for ele in sorted_list:
                if ele.overlap_ratio > 5.0:
                    res = True
                fout[ele.class_index].write(
                    imgid
                    + " "
                    + str(ele.local_search_index)
                    + " "
                    + str(ele.box[0])
                    + " "
                    + str(ele.box[1])
                    + " "
                    + str(ele.box[2])
                    + " "
                    + str(ele.box[3])
                    + "\n"
                )
            if res:
                count = count + 1
                print "Hitted"
            else:
                print "Missed"
            index = index + 1
        print "Hitted count, Upper Count,  Total: %d, %d, %d" % (count, upper_count, total)
y_train = y_train[selector]


print "size of dataset is : %s" % len(y)
print "now training..."

####################
# Training the CNN #
####################
max_w = X.shape[1]
print max_w

x_train = np.reshape(x_train, [-1, max_w, 320, 1])
x_test = np.reshape(x_test, [-1, max_w, 320, 1])

cnn = CNN(input_shape=[max_w, 320, 1], classes=np.unique(y), conv_shape=[4, 55], epochs=25000)
cnn.fit(x_train, y_train, x_test, y_test)

print "done training"
print "testing"

# Testing :
###########
y_pred = np.array([])

for c, t in enumerate(Batcher.chunks(x_test, 100)):
    y_pred = np.append(y_pred, cnn.predict(t))

classification_rep = classification_report(y_test, y_pred)
print classification_rep

# Reading mnist Data :
######################
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# Training :
############
x_train = np.reshape(mnist.train.images, [-1, 28, 28, 1])

# converting mnist correct labels 1 hot vectors into data into ids of correct labels
y_train = mnist.train.labels
y_train = [np.where(i == 1)[0][0] for i in y_train]

classes = np.unique(y_train)
cnn = CNN(input_shape=[28, 28, 1], classes=classes, conv_shape=[5, 5])

cnn.fit(x_train, y_train)

# Testing :
###########
x_test = np.reshape(mnist.test.images, [-1, 28, 28, 1])
y_pred = cnn.predict(x_test)

y_true = mnist.test.labels
y_true = [list(i).index(1) for i in y_true]

print classification_report(y_true, y_pred)