def  predict(models_path,image_path,labels_filename,labels_nums, data_format):
    #[batch_size, resize_height, resize_width, depths] = data_format
    tf.reset_default_graph()
    #labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input')

    with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
        out, end_points = alaxnet.alexnet_v2(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=1.0, is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out,name='pre')
    class_id = tf.argmax(score, 1)

    #
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)



    im=read_image(image_path,resize_height,resize_width,normalization=True)
    im=im[np.newaxis,:]
    pre_score,pre_label = sess.run([score,class_id], feed_dict={input_images:im})

    sess.close()

    return pre_label[0]
Пример #2
0
def  predict(models_path,image_dir,labels_filename,labels_nums, data_format):
    [batch_size, resize_height, resize_width, depths] = data_format

    labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input')

    with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
        out, end_points = alaxnet.alexnet_v2(inputs=input_images, num_classes=labels_nums, dropout_keep_prob=1.0, is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out,name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    images_list=glob.glob(os.path.join(image_dir,'*.jpg'))
    score_total = 0
    for image_path in images_list:
        im=read_image(image_path,resize_height,resize_width,normalization=True)
        im=im[np.newaxis,:]
        #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score,pre_label = sess.run([score,class_id], feed_dict={input_images:im})
        max_score=pre_score[0,pre_label]
        #print("{} is: pre labels:{},name:{} score: {}".format(image_path, pre_label, labels[pre_label], max_score))
        if image_path.split(".jpg")[0].split("-")[2] == labels[pre_label]:
            score_total += 1
            print("{} is predicted as label::{} ".format(image_path, labels[pre_label]))
        else:
            print("{} is predicted as label::{} ".format(image_path,labels[pre_label]))

    print("valuation accuracy is {}".format(score_total/len(images_list)))
    sess.close()
Пример #3
0
def predict(image_dir, onset_frames, onsets_frames_strength, models_path):
    class_nums = 2
    onsets = []
    onsets_strength = {}

    batch_size = 1  #
    resize_height = 224  # 指定存储图片高度
    resize_width = 224  # 指定存储图片宽度
    depths = 3
    data_format = [batch_size, resize_height, resize_width, depths]

    [batch_size, resize_height, resize_width, depths] = data_format

    #labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(
        dtype=tf.float32,
        shape=[None, resize_height, resize_width, depths],
        name='input')

    with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
        out, end_points = alaxnet.alexnet_v2(inputs=input_images,
                                             num_classes=class_nums,
                                             dropout_keep_prob=1.0,
                                             is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out, name='pre')
    class_id = tf.argmax(score, 1)

    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)
    images_list = sorted(glob.glob(os.path.join(image_dir, '*.jpg')),
                         key=os.path.getmtime)
    #sorted(glob.glob('*.png'), key=os.path.getmtime)
    score_total = 0
    index = 0
    for image_path in images_list:
        im = read_image(image_path,
                        resize_height,
                        resize_width,
                        normalization=True)
        im = im[np.newaxis, :]
        #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
        pre_score, pre_label = sess.run([score, class_id],
                                        feed_dict={input_images: im})
        max_score = pre_score[0, pre_label]
        #print("{} is: pre labels:{},name:{} score: {}".format(image_path, pre_label, labels[pre_label], max_score))
        print("{} is predicted as label::{} ".format(image_path, pre_label[0]))
        if 1 == pre_label[0]:
            #score_total += 1
            onsets.append(onset_frames[index])
            onsets_strength[onset_frames[index]] = onsets_frames_strength.get(
                onset_frames[index])
        else:
            pass
        index += 1
    print("valuation accuracy is {}".format(score_total / len(images_list)))
    sess.close()
    return onsets, onsets_strength
Пример #4
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    train_images_batch, train_labels_batch = get_batch_images(
        train_record_file)
    # val数据,验证数据可以不需要打乱数据
    val_images_batch, val_labels_batch = get_batch_images(val_record_file)

    # Define the model:
    with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
        out, end_points = alexnet.alexnet_v2(inputs=input_images,
                                             num_classes=labels_nums,
                                             dropout_keep_prob=keep_prob,
                                             is_training=is_training)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels,
                                    logits=out)  #添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(
        add_regularization_losses=True)  #添加正则化损失loss=2.2
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))

    # Specify the optimization scheme:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)
    # create_train_op that ensures that when we evaluate it to get the loss,
    # the update_ops are done and the gradient updates are computed.
    train_op = slim.learning.create_train_op(total_loss=loss,
                                             optimizer=optimizer)

    # global_step = tf.Variable(0, trainable=False)
    # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9)
    #
    # optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
    # # train_op = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)

    saver = tf.train.Saver()
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_input_images, batch_input_labels = sess.run(
                [train_images_batch, train_labels_batch])
            _, train_loss = sess.run(
                [train_op, loss],
                feed_dict={
                    input_images: batch_input_images,
                    input_labels: batch_input_labels,
                    keep_prob: 0.5,
                    is_training: True
                })
            # train测试(这里仅测试训练集的一个batch)
            if i % train_log_step == 0:
                train_acc = sess.run(accuracy,
                                     feed_dict={
                                         input_images: batch_input_images,
                                         input_labels: batch_input_labels,
                                         keep_prob: 1.0,
                                         is_training: False
                                     })
                print(
                    "%s: Step [%d]  train Loss : %f, training accuracy :  %g" %
                    (datetime.now(), i, train_loss, train_acc))

            # val测试(测试全部val数据)
            if i % val_log_step == 0:
                mean_loss, mean_acc = net_evaluation(sess, loss, accuracy,
                                                     val_images_batch,
                                                     val_labels_batch,
                                                     val_nums)
                print("%s: Step [%d]  val Loss : %f, val accuracy :  %g" %
                      (datetime.now(), i, mean_loss, mean_acc))

            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # 保存val准确率最高的模型
            if mean_acc > max_acc and mean_acc > 0.5:
                max_acc = mean_acc
                path = os.path.dirname(snapshot_prefix)
                best_models = os.path.join(
                    path, 'best_models_{}_{:.4f}.ckpt'.format(i, max_acc))
                print('------save:{}'.format(best_models))
                saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
Пример #5
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    train_nums = get_example_nums(train_record_file)
    val_nums = get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    regularizer = slim.l2_regularizer(0.0005)
    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    train_images, train_labels = read_records(train_record_file,
                                              resize_height,
                                              resize_width,
                                              type='normalization')
    train_images_batch, train_labels_batch = get_batch_images(
        train_images,
        train_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=True,
        shuffle=True)
    # val数据,验证数据可以不需要打乱数据
    val_images, val_labels = read_records(val_record_file,
                                          resize_height,
                                          resize_width,
                                          type='normalization')
    val_images_batch, val_labels_batch = get_batch_images(
        val_images,
        val_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=True,
        shuffle=False)

    # Define the model:
    with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
        out, end_points = alaxnet.alexnet_v2(inputs=input_images,
                                             num_classes=labels_nums,
                                             dropout_keep_prob=keep_prob,
                                             is_training=is_training)

    # Specify the loss function: tf.losses定义的loss函数都会自动添加到loss函数,不需要add_loss()了
    tf.losses.softmax_cross_entropy(onehot_labels=input_labels,
                                    logits=out)  #添加交叉熵损失loss=1.6
    # slim.losses.add_loss(my_loss)
    loss = tf.losses.get_total_loss(
        add_regularization_losses=True)  #添加正则化损失loss=2.2
    accuracy = tf.reduce_mean(
        tf.cast(tf.equal(tf.argmax(out, 1), tf.argmax(input_labels, 1)),
                tf.float32))

    score = tf.nn.softmax(out, name='score')
    classIds = tf.argmax(out, 1)
    # Specify the optimization scheme:
    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)

    # global_step = tf.Variable(0, trainable=False)
    # learning_rate = tf.train.exponential_decay(0.05, global_step, 150, 0.9)
    #
    optimizer = tf.train.MomentumOptimizer(learning_rate=base_lr, momentum=0.9)
    # # train_tensor = optimizer.minimize(loss, global_step)
    # train_op = slim.learning.create_train_op(loss, optimizer,global_step=global_step)

    # 在定义训练的时候, 注意到我们使用了`batch_norm`层时,需要更新每一层的`average`和`variance`参数,
    # 更新的过程不包含在正常的训练过程中, 需要我们去手动像下面这样更新
    # 通过`tf.get_collection`获得所有需要更新的`op`
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # 使用`tensorflow`的控制流, 先执行更新算子, 再执行训练
    with tf.control_dependencies(update_ops):
        # create_train_op that ensures that when we evaluate it to get the loss,
        # the update_ops are done and the gradient updates are computed.
        # train_op = slim.learning.create_train_op(total_loss=loss,optimizer=optimizer)
        train_op = slim.learning.create_train_op(total_loss=loss,
                                                 optimizer=optimizer)

    # 循环迭代过程
    step_train(train_op, loss, accuracy, score, classIds, train_images_batch,
               train_labels_batch, train_nums, train_log_step,
               val_images_batch, val_labels_batch, val_nums, val_log_step,
               snapshot_prefix, snapshot)
Пример #6
0
def  predict(filename,image_dir,onset_frames,onsets_frames_strength,models_path,f_range):
    if onset_frames:
        class_nums = 2
        onsets = []
        onsets_strength = {}
        tf.reset_default_graph()

        batch_size = 1  #
        resize_height = 224  # 指定存储图片高度
        resize_width = 224  # 指定存储图片宽度
        depths = 3
        data_format = [batch_size, resize_height, resize_width, depths]

        [batch_size, resize_height, resize_width, depths] = data_format

        #labels = np.loadtxt(labels_filename, str, delimiter='\t')
        input_images = tf.placeholder(dtype=tf.float32, shape=[None, resize_height, resize_width, depths], name='input')

        with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
            out, end_points = alaxnet.alexnet_v2(inputs=input_images, num_classes=class_nums, dropout_keep_prob=1.0, is_training=False)

        # 将输出结果进行softmax分布,再求最大概率所属类别
        score = tf.nn.softmax(out,name='pre')
        class_id = tf.argmax(score, 1)

        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        saver.restore(sess, models_path)
        images_list=sorted(glob.glob(os.path.join(image_dir,'*.png')), key=os.path.getmtime)
        # images_list = glob.glob(os.path.join(image_dir, '*.png'))
        #sorted(glob.glob('*.png'), key=os.path.getmtime)
        score_total = 0
        index = 0
        key_type = type(list(onsets_frames_strength.keys())[0])
        for image_path in images_list:
            im=read_image(image_path,resize_height,resize_width,normalization=True)
            im=im[np.newaxis,:]
            #pred = sess.run(f_cls, feed_dict={x:im, keep_prob:1.0})
            pre_score,pre_label = sess.run([score,class_id], feed_dict={input_images:im})
            max_score=pre_score[0,pre_label]
            #print("{} is: pre labels:{},name:{} score: {}".format(image_path, pre_label, labels[pre_label], max_score))
            print("{} is predicted as label::{} ".format(image_path,pre_label[0]))

            # 将判断为yes的节拍加入onsets
            if 1 == pre_label[0]:
                #score_total += 1
                onsets.append(onset_frames[index])
                onsets_strength[key_type(onset_frames[index])] = onsets_frames_strength.get(key_type(onset_frames[index]))
            else:
                pass
            index += 1
        #accuracy = 0
        #print("valuation accuracy is {}".format(accuracy))
        sess.close()
        # if len(onsets)!=0:
        #     y, sr = librosa.load(filename)
        #     rms = librosa.feature.rmse(y=y)[0]
        #     rms = [x / np.std(rms) for x in rms]
        #     onsets,onsets_strength = remove_crowded_frames_by_rms(onsets,onsets_strength,rms,int(f_range*2)+2)
        return onsets,onsets_strength#,accuracy
    return [],{}
Пример #7
0
def predict(models_path, labels_nums, image_dir):
    #[batch_size, resize_height, resize_width, depths] = data_format
    resize_height = 224  # 指定存储图片高度
    resize_width = 224  # 指定存储图片宽度
    depths = 3
    tf.reset_default_graph()
    #labels = np.loadtxt(labels_filename, str, delimiter='\t')
    input_images = tf.placeholder(
        dtype=tf.float32,
        shape=[None, resize_height, resize_width, depths],
        name='input')

    with slim.arg_scope(alaxnet.alexnet_v2_arg_scope()):
        out, end_points = alaxnet.alexnet_v2(inputs=input_images,
                                             num_classes=labels_nums,
                                             dropout_keep_prob=1.0,
                                             is_training=False)

    # 将输出结果进行softmax分布,再求最大概率所属类别
    score = tf.nn.softmax(out, name='pre')
    class_id = tf.argmax(score, 1)

    #
    sess = tf.InteractiveSession()
    # sess.list_devices()
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver()
    saver.restore(sess, models_path)

    image_list = os.listdir(image_dir)
    error_list = []
    correct_list = []
    all_indexs = get_indexs_from_filename(image_list)
    c = 0

    for i in all_indexs:
        image_name = str(i) + ".jpg"
        image_path = image_dir + image_name
        index = int(image_name.split('.jpg')[0])
        # image_path = image_path.replace("\\",os.altsep).replace("/",os.altsep)
        # print("index is {}".format(index))
        if c == 0 or index > c:
            # if True:
            im = read_image(image_path,
                            resize_height,
                            resize_width,
                            normalization=True)
            im = im[np.newaxis, :]
            pre_score, pre_label = sess.run([score, class_id],
                                            feed_dict={input_images: im})
            if pre_label == 1:
                correct_list.append(index)
                c, middle_position = get_last_nearly_index(index, all_indexs)
                # print("=============")
            else:
                error_list.append(image_name)
    correct_list.sort()

    sess.close()

    return correct_list