def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            resize_rsp = resize_image(
                Image.open(os.path.join(app.config['UPLOAD_FOLDER'],
                                        filename)))
            crop_rsp = crop_center(resize_rsp)
            grayscale_rsp = image_to_grayscale_pixel_values(crop_rsp)
            standardize_rsp = standardize_pixels(grayscale_rsp)
            predicted_label, probabilities = CNN(standardize_rsp)
            total = sum(probabilities[0])
            percentages = [
                round((x / float(total)) * 100, 2) for x in probabilities[0]
            ]
            labels = [
                'Anger', 'Disgust', 'Fear', 'Happiness', 'Sadness', 'Surprise',
                'Neutral'
            ]
            rsp_img = url_for('uploaded_file', filename=filename)
            return render_template('index.html',
                                   rsp_img=rsp_img,
                                   rsp_label=labels[predicted_label],
                                   rsp_dict=zip(labels, percentages))

    return render_template('index.html')
示例#2
0
def test(save_path, test_x, test_y, vis, test_scenario='test', n_way=3, shot=3, eval_mode=False):
    net = CNN(nc=n_way).to(device)
    net.load_state_dict(torch.load(save_path))
    if eval_mode:
        net.eval()
    print("Load the model Successfully!\n%s" % save_path)
    loss = nn.CrossEntropyLoss()

    n_examples = test_x.shape[1]
    n_class = test_x.shape[0]
    assert test_x.shape[1] == test_x.shape[1]
    assert n_way == n_class

    print('n_way=>', n_way, 'n_shot/bsize=>', shot)
    print('test_x Shape:', test_x.shape)
    print('test_y Shape:', test_y.shape)
    print("---------------------Testing----------------------\n")
    avg_acc_ = 0.
    avg_loss_ = 0.
    n_episodes = n_examples // shot
    n_epochs = 1
    counter = 0
    for ep in range(n_epochs):
        avg_acc = 0.
        avg_loss = 0.
        count = 0
        for epi in range(n_episodes):
            x, y = test_x[:, count:count + shot], test_y[:, count:count + shot]
            x, y = x.to(device), y.contiguous().view(-1).to(device)
            count += shot

            outputs = net.forward(x)
            losses = loss(outputs, y)
            ls = losses.cpu().item()
            ac = compute_acc(outputs, y)
            avg_acc += ac
            avg_loss += ls
            print('[test episode {}/{}] => loss:{:.6f}, acc: {:.6f}'.format(epi + 1, n_episodes, ls, ac))
            vis.line(Y=[[ac, ls]], X=[counter],
                     update=None if counter == 0 else 'append', win=test_scenario,
                     opts=dict(legend=['accuracy', 'loss'], title=test_scenario))
            counter += 1
        avg_acc /= n_episodes
        avg_loss /= n_episodes
        avg_acc_ += avg_acc
        avg_loss_ += avg_loss
        print('[epoch {}/{}] => avg_loss: {:.8f}, avg_acc: {:.8f}'.format(ep + 1, n_epochs, avg_loss, avg_acc))
    avg_acc_ /= n_epochs
    avg_loss_ /= n_epochs
    vis.text('Average Accuracy: {:.6f}  Average Loss:{:.6f}'.format(avg_acc_, avg_loss_), win='Test result')
    print('\n------------------------Average Result----------------------------')
    print('Average Test Accuracy: {:.6f}'.format(avg_acc_))
    print('Average Test Loss: {:.6f}\n'.format(avg_loss_))
示例#3
0
 def run_test(self, x_test):
     #self.json_config = os.path.join(self.save_result, 'config.json')
     print("Predicting data...")
     x_test = numpy.array(x_test).reshape(-1,2048)
     x_test = enelop_trans(x_test)
     print('X_test shape is: ', x_test.shape)
     self.config = Config()
     self.model = CNN(self.config)
     with tf.Session(config=self.gpu_config) as sess_test:
         tf.global_variables_initializer().run()
         saver = tf.train.Saver()
         saver.restore(sess=sess_test, save_path=self.save_path)  # 读取保存的模型
         feed_dict = feed_test_data(x_test, 1.0, False)   # EVAL: keep_prob=1.0, valuation & testing are not during training.
         y_test_pred = sess_test.run(self.model.y_pred, feed_dict=feed_dict)
         print('y_test_pred',y_test_pred)
示例#4
0
def main(save_path, scenario, normalization=True, ls_threshold=0.001,
         n_way=3, samples=20, shot=3, split=10, eval_=False):
    """
    train and test
    :param eval_:
    :param samples:
    :param scenario:
    :param save_path:
    :param normalization:
    :param ls_threshold:
    :param n_way:
    :param shot:
    :param split:
    """
    print('%d GPU is available.' % torch.cuda.device_count())
    net = CNN(nc=n_way).to(device)
    vis = visdom.Visdom(env='yancy_env')
    # print(net.cpu())
    # train_x, train_y, test_x, test_y = generator.Cs_data_generator2(examples=samples, split=split, way=n_way,
    #                                                                 normalize=normalization, label=True)
    train_x, train_y, test_x, test_y = generator.SQ_data_generator2(examples=100, split=split, way=n_way,
                                                                    normalize=normalization, label=True)
    # train_x, train_y, test_x, test_y = generator.SQ_data_generator3(examples=samples, split=split, way=n_way,
    #                                                                 normalize=normalization, label=True)
    n_class = train_x.shape[0]
    assert n_class == n_way
    print('train_x Shape:', train_x.shape)
    print('train_y Shape:', train_y.shape)
    print('test_x Shape:', test_x.shape)
    print('test_y Shape:', test_y.shape)
    train_x, train_y = torch.from_numpy(train_x).float(), torch.from_numpy(train_y).long()
    test_x, test_y = torch.from_numpy(test_x).float(), torch.from_numpy(test_y).long()

    order = input("Train or not? Y/N\n")
    if order == 'Y' or order == 'y':
        if os.path.exists(save_path):
            print('The training file exists:%s' % save_path)
        else:
            train(net=net, save_path=save_path, train_x=train_x, train_y=train_y, vis=vis,
                  train_scenario=scenario, ls_threshold=ls_threshold, n_way=n_way, shot=shot)

    order = input("Test or not? Y/N\n")
    if order == 'Y' or order == 'y':
        if os.path.exists(save_path):
            test(save_path=save_path, test_x=test_x, test_y=test_y, vis=vis,
                 n_way=n_way, shot=shot, eval_mode=eval_)
        else:
            print('The path does NOT exist! Check it please:%s' % save_path)
示例#5
0
    def __init__(self,
                 embedding_dim,
                 hidden_dim,
                 vocab_size,
                 encode_dim,
                 label_size,
                 batch_size,
                 use_gpu=True,
                 pretrained_weight=None,
                 model=None):
        super(BatchProgramCC, self).__init__()
        self.model = model
        self.stop = [vocab_size - 1]
        self.hidden_dim = hidden_dim
        self.num_layers = 1
        self.gpu = use_gpu
        self.batch_size = batch_size
        self.vocab_size = vocab_size
        self.embedding_dim = embedding_dim
        self.encode_dim = encode_dim
        self.label_size = label_size
        self.encoder = BatchTreeEncoder(self.vocab_size, self.embedding_dim,
                                        self.encode_dim, self.batch_size,
                                        self.gpu, pretrained_weight)
        self.root2label = nn.Linear(self.encode_dim, self.label_size)
        # gru
        self.bigru = nn.GRU(self.encode_dim,
                            self.hidden_dim,
                            num_layers=self.num_layers,
                            bidirectional=True,
                            batch_first=True)
        self.cnn = CNN()

        self.pooling2label = nn.Linear(self.hidden_dim * 2, self.label_size)

        # attention linear
        self.hidden2label = nn.Linear(self.hidden_dim * 2 * 30,
                                      self.label_size)

        self.cnn2label = nn.Linear(self.hidden_dim, self.label_size)

        # hidden
        self.hidden = self.init_hidden()
        self.dropout = nn.Dropout(0.2)

        # 关于attention的
        self.W_s1 = nn.Linear(2 * self.hidden_dim, 350)
        self.W_s2 = nn.Linear(350, 30)
示例#6
0
        print('Current working space is: ', os.getcwd())  # 返回当前工作目录。
        print('Configuring and Saving CNN model...')
        # save_path = os.path.join(save_dir, 'best_validation')  # 最佳验证结果保存路径
        # 检查路径是否存在,不存在则创建一个

        for path in [epoch_valid_tensorboard_dir, iter_train_tensorboard_dir, save_dir, save_result_dir]:
            if not os.path.exists(path):
                os.makedirs(path)

        Config.learning_rate = j  # 改变Config中默认的learning_rate
        Config.dropout_keep_prob_fc = k  # 改变Config中默认的神经元保留率
        Config.batch_size = bat

        config = Config()
        model = CNN(config)  # 调用 CNN 类 实例化,self.acc -> model.acc

        print("===============================CNN Model is Finished=======================")
        print("========================= run_main_function is training =======================")
        # ================================================================================================
        # 保存结果到本地
        '''需要注意的是,tf.summary.merge_all()要写在tf.summary.scalar()
        或是tf.summary.histogram()等方法之后,
        不然会报Fetch argument None has invalid type<class 'NoneType'>的错。
        '''
        merged = tf.summary.merge_all()  # 可以将所有summary全部保存到磁盘,以便tensorboard显示。
        train_writer = tf.summary.FileWriter(iter_train_tensorboard_dir)  # 定义一个写入summary的目标文件,dir为写入文件地址
        valid_writer = tf.summary.FileWriter(epoch_valid_tensorboard_dir)

        # 在训练集上训练, 在验证集上评估
        run_main(train_data, train_label, train_ratio=config.RATIO_train)  # 训练集中,训练集与验证集再划分为 4:1
示例#7
0
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize((0.1307, ), (0.3081, ))
        ]),
        fraction=f),
                                               batch_size=128,
                                               shuffle=True)
    test_loader = torch.utils.data.DataLoader(MNISTDataset(
        '../Data Processing/processed_test_mnist.npz',
        transform=torchvision.transforms.Compose([
            torchvision.transforms.ToTensor(),
            torchvision.transforms.Normalize((0.1307, ), (0.3081, ))
        ])),
                                              batch_size=500,
                                              shuffle=True)

    network = CNN()
    optimizer = optim.Adam(network.parameters(), lr=0.001)
    if gpu:
        network = network.cuda()

    n_epochs = 30
    log_interval = 200
    test_interval = 3
    train_losses = []
    train_counter = []
    test_losses = []
    test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)]

    def train(epoch):
        network.train()
        for batch_idx, (data, target) in enumerate(train_loader):
                                    range(Config.repeat), 
                                    Config.learning_rate_list,
                                    Config.dropout_keep_prob_fc_list):

        print('Current working space is: ', os.getcwd())
        print('Configuring and Saving CNN model...')

        for path in [tensorboard_dir, save_dir, save_result]:
            if not os.path.exists(path):
                os.makedirs(path)

        Config.learning_rate = j
        Config.dropout_keep_prob_fc = k

        config = Config()
        model = CNN(config)

        # copy Config.py to save_result

        # print(x_train.size)
        train(x_train, y_train, train_ratio=config.RATIO_train)
        acc_test, loss_test = test(x_test, y_test)

        results.append(js_methods(i, j, k, acc_test, loss_test))
        
        shutil.copy('Config.py', save_result)
        shutil.copy('CNN_model.py', save_result)
        shutil.copy('run.py', save_result)

        #TestForChangeToWrite('G:/js/code/model/tensorboard')
        #TestForChangeToWrite(save_dir)