コード例 #1
0
    dataset, [train_size, test_size])

train_loader = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=batch_size,
                                           shuffle=False,
                                           num_workers=0)
test_loader = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False,
                                          num_workers=0)

## LOAD previous cnn
# cnn.load(PATH_TO_LOAD)

## TRAIN and SAVE the CNN
train_result = train_cnn(epochs, cnn, criterion, train_loader, optimizer,
                         batch_rate)
cnn.save(PATH_TO_SAVE)

## TEST the CNN
test_cnn(cnn, test_loader)

## CHECK the CNN
check_cnn(cnn, test_loader)

## Show the respective graphs
show_statistics(cnn, test_loader, train_result)

## Generate the scv with test data
generate(cnn)

## Make random prediction with the cnn
コード例 #2
0
                   Inception(512, 112, (144, 288), (32, 64), 64),
                   Inception(528, 256, (160, 320), (32, 128), 128),
                   nn.MaxPool2d(kernel_size=3, stride=2, padding=1))

b5 = nn.Sequential(Inception(832, 256, (160, 320), (32, 128), 128),
                   Inception(832, 384, (192, 384), (48, 128), 128),
                   utils.GlobalAvgPool2d())

print('查看网络结构')

net = nn.Sequential(b1, b2, b3, b4, b5, utils.FlattenLayer(),
                    nn.Linear(1024, 10))
X = torch.rand(1, 1, 96, 96)
for blk in net.children():
    X = blk(X)
    print('output shape: ', X.shape)

print('获取和读取数据,这里缩减尺寸为 96')
batch_size = 256
train_iter, test_iter = utils.load_data_fashion_mnist(batch_size, resize=96)

print('训练模型,只 1 轮')
lr, num_epochs = 0.002, 1
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
utils.train_cnn(net, train_iter, test_iter, batch_size, optimizer, device,
                num_epochs)
'''
训练模型,只 1 轮
training on cpu
epoch 1, loss 0.0058, train acc 0.414, test acc 0.754, time 2282.1 sec
'''
コード例 #3
0
def train():
    focus = 'histogram' if request.form['focus'] == 'Histograma' else 'pixel'
    n_neighbors = request.form['n_neighbors']
    max_iter_svm = request.form['max_iter_svm']
    hidden_layer_sizes = request.form['hidden_layer_sizes']
    max_iter_bpnn = request.form['max_iter_bpnn']
    epochs = request.form['epochs']
    steps_per_epoch = request.form['steps_per_epoch']
    validation_steps = request.form['validations_steps']
    training_steps = request.form['training_steps']
    class_positive = request.form['class_positive'].upper()
    class_negative = request.form['class_negative'].upper()
    response_cnn = utils.train_cnn(steps_per_epoch, epochs, validation_steps,
                                   class_positive, class_negative)
    if response_cnn['success'] == True:
        response_svm_knn_bpnn = utils.train_svm_knn_bpnn(
            n_neighbors, focus, hidden_layer_sizes, max_iter_bpnn,
            max_iter_svm)
        if response_svm_knn_bpnn['success'] == True:
            if app.config['RUN_IMAGE_RETRAINING'] == True:
                response_image_retraining = utils.train_image_retraining(
                    training_steps)
                if response_image_retraining['success'] == True:
                    return render_template(
                        'form_predict.html',
                        val_loss_cnn=round(
                            Decimal(response_cnn['val_loss']) * 100),
                        val_acc_cnn=round(
                            Decimal(response_cnn['val_acc']) * 100),
                        loss_cnn=round(Decimal(response_cnn['loss']) * 100),
                        acc_cnn=round(Decimal(response_cnn['acc']) * 100),
                        positive_class=response_cnn['positive_class'],
                        negative_class=response_cnn['negative_class'],
                        length_images=response_svm_knn_bpnn['length_images'],
                        size_package=response_svm_knn_bpnn['size_package'],
                        accuracy_knn=response_svm_knn_bpnn['accuracy_knn'],
                        accuracy_bpnn=response_svm_knn_bpnn['accuracy_bpnn'],
                        accuracy_svm=response_svm_knn_bpnn['accuracy_svm'])
                else:
                    return jsonify({
                        'success': False,
                        'msg': 'error-train-image-retraining'
                    })
            else:
                return render_template(
                    'form_predict.html',
                    val_loss_cnn=round(
                        Decimal(response_cnn['val_loss']) * 100),
                    val_acc_cnn=round(Decimal(response_cnn['val_acc']) * 100),
                    loss_cnn=round(Decimal(response_cnn['loss']) * 100),
                    acc_cnn=round(Decimal(response_cnn['acc']) * 100),
                    positive_class=response_cnn['positive_class'],
                    negative_class=response_cnn['negative_class'],
                    length_images=response_svm_knn_bpnn['length_images'],
                    size_package=response_svm_knn_bpnn['size_package'],
                    accuracy_knn=response_svm_knn_bpnn['accuracy_knn'],
                    accuracy_bpnn=response_svm_knn_bpnn['accuracy_bpnn'],
                    accuracy_svm=response_svm_knn_bpnn['accuracy_svm'])
        else:
            return jsonify({
                'success': False,
                'msg': 'error-train-svm-knn-bpnn'
            })
    else:
        return jsonify({'success': False, 'msg': 'error-train-cnn'})