Пример #1
0
def CreateModel(model_name, bit, use_gpu):
    if model_name == 'vgg11':
        vgg11 = models.vgg11(pretrained=True)
        cnn_model = CNN_model.cnn_model(vgg11, model_name, bit)
    if model_name == 'alexnet':
        alexnet = models.alexnet(pretrained=True)
        cnn_model = CNN_model.cnn_model(alexnet, model_name, bit)
    if use_gpu:
        cnn_model = cnn_model.cuda()
    return cnn_model
Пример #2
0
def get_cnn_model(model_name, bits):
    if model_name == 'vgg11':
        vgg11 = models.vgg11(pretrained=True)
        cnn_model = CNN_model.cnn_model(vgg11, model_name, bits)
    if model_name == 'alexnet':
        alexnet = models.alexnet(pretrained=True)
        cnn_model = CNN_model.cnn_model(alexnet, model_name, bits)
    if model_name == 'resnet':
        cnn_model = CNN_model.getResnetModel(bits)
    if torch.cuda.is_available():
        cnn_model = cnn_model.cuda()
    return cnn_model
Пример #3
0
def CreateModel(model_name, bit, use_gpu):
    if model_name == 'vgg11':
        vgg11 = models.vgg16(pretrained=True)
        cnn_model = CNN_model.cnn_model(vgg11, model_name, bit)
    if model_name == 'alexnet':
        alexnet = models.alexnet(pretrained=True)
        cnn_model = CNN_model.cnn_model(alexnet, model_name, bit)
    if model_name == 'resnet34':
        resnet = models.resnet34(pretrained=True)
        cnn_model = CNN_model.cnn_model(resnet, model_name, bit)
    if model_name == 'resnet50':
        resnet = models.resnet50(pretrained=True)
        cnn_model = CNN_model.cnn_model(resnet, model_name, bit)
    if model_name == 'resnet34resnet34':
        resnet = models.resnet34(pretrained=True)
        cnn_model = CNN_model.cnn_model(resnet, model_name, bit)

    return cnn_model
Пример #4
0
def CreateModel(model_name, bit, use_gpu):
    if model_name == 'vgg16':
        vgg16 = models.vgg16(pretrained=True)
        #vgg16 = torch.load('/home/zhangjingyi/Rescode/vgg16_caffe2pytorch/vgg16_20M.pkl')
        cnn_model = CNN_model.cnn_model(vgg16, model_name, bit)
    print('**********************************')
    print('Def of poo4~poo5:')
    for i in [23, 24, 26, 28, 30]:
        print(cnn_model.features[i])
    print('**********************************')
    if use_gpu:
        cnn_model = torch.nn.DataParallel(cnn_model).cuda()
        #cnn_model = cnn_model.cuda()
    return cnn_model
Пример #5
0
def train_net(initial_learning_rate, loss_mode, optimizer_mode, data):

    tf.reset_default_graph()
    train_data, test_desired_input, test_desired_output = data

    desired_input, desired_output, layer9, keep_prob = CNN_model.cnn_model(
    ).cnn_onemap_model()
    model_name = "cnn_onemap_model"

    global_ = tf.Variable(tf.constant(0))
    decay_learning_rate = tf.train.exponential_decay(initial_learning_rate,
                                                     global_,
                                                     5,
                                                     0.95,
                                                     staircase=True)

    loss, loss_name = loss_function(loss_mode, desired_output, layer9)
    tf.summary.scalar("loss", loss)

    train_step = optimizer(optimizer_mode,
                           decay_learning_rate)[0].minimize(loss)
    optimizer_name = optimizer(optimizer_mode, decay_learning_rate)[1]

    correct_prediction = tf.equal(tf.arg_max(desired_output, 1),
                                  tf.arg_max(layer9, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar("accuracy", accuracy)

    #tensorboard merged all
    merged = tf.summary.merge_all()

    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)

    writer = tf.summary.FileWriter(
        LOGDIR + model_name + '_' + str(initial_learning_rate) + '_' +
        loss_name + '_' + optimizer_name + "/", sess.graph)
    writer.add_graph(sess.graph)

    data_file = open(
        LOGDIR + '/' + str(initial_learning_rate) + '_' + loss_name + '_' +
        optimizer_name + ".txt", 'w')

    for i in range(2):

        minibatch = random.sample(train_data, 100)
        image_batch = [d[0] for d in minibatch]
        label_batch = [d[1] for d in minibatch]

        sess.run(train_step,
                 feed_dict={
                     desired_input: image_batch,
                     desired_output: label_batch,
                     keep_prob: 0.5
                 })
        sess.run(decay_learning_rate, feed_dict={global_: i})

        training_loss = sess.run(loss,
                                 feed_dict={
                                     desired_input: image_batch,
                                     desired_output: label_batch,
                                     keep_prob: 1
                                 })
        test_loss = sess.run(loss,
                             feed_dict={
                                 desired_input: test_desired_input,
                                 desired_output: test_desired_output,
                                 keep_prob: 1.0
                             })

        accuracy_num = sess.run(accuracy,
                                feed_dict={
                                    desired_input: test_desired_input,
                                    desired_output: test_desired_output,
                                    keep_prob: 1.0
                                })
        #accuracy_num = sess.run(accuracy,feed_dict={desired_input:image_batch,take_desired_output:label_batch,keep_prob: 1.0})
        print("step:", i, "accuracy:", accuracy_num, "training_loss",
              training_loss, "test_loss", test_loss)
        data_file.write(
            str(i) + ',' + str(accuracy_num) + ',' + str(training_loss) + ',' +
            str(test_loss) + '\n')
        summary_data = sess.run(merged,
                                feed_dict={
                                    desired_input: image_batch,
                                    desired_output: label_batch,
                                    keep_prob: 1
                                })
        writer.add_summary(summary_data, i)