愿你 发表于 2020-3-28 14:55:33

为什么出现错误if src_img.ndim != 3: AttributeError: 'NoneType' object has no ...

本帖最后由 一个账号 于 2020-3-28 15:22 编辑

import sys
import os
import numpy as np
import cv2
import tensorflow as tf

from sklearn.model_selection import train_test_split


class plate_cnn_net:
    def __init__(self):
      self.img_w,self.img_h = 136,36
      self.y_size = 2
      self.batch_size = 100
      self.learn_rate = 0.001

      self.x_place = tf.placeholder(dtype=tf.float32, shape=, name='x_place')
      self.y_place = tf.placeholder(dtype=tf.float32, shape=, name='y_place')
      self.keep_place = tf.placeholder(dtype=tf.float32, name='keep_place')

    def cnn_construct(self):
      x_input = tf.reshape(self.x_place, shape=[-1, self.img_h, self.img_w, 3])

      cw1 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb1 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x_input, filter=cw1, strides=, padding='SAME'), cb1))
      conv1 = tf.nn.max_pool(conv1, ksize=, strides=, padding='SAME')
      conv1 = tf.nn.dropout(conv1, self.keep_place)

      cw2 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb2 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, filter=cw2, strides=, padding='SAME'), cb2))
      conv2 = tf.nn.max_pool(conv2, ksize=, strides=, padding='SAME')
      conv2 = tf.nn.dropout(conv2, self.keep_place)

      cw3 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb3 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, filter=cw3, strides=, padding='SAME'), cb3))
      conv3 = tf.nn.max_pool(conv3, ksize=, strides=, padding='SAME')
      conv3 = tf.nn.dropout(conv3, self.keep_place)

      conv_out = tf.reshape(conv3, shape=[-1, 17 * 5 * 128])

      fw1 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb1 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully1 = tf.nn.relu(tf.add(tf.matmul(conv_out, fw1), fb1))
      fully1 = tf.nn.dropout(fully1, self.keep_place)

      fw2 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb2 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully2 = tf.nn.relu(tf.add(tf.matmul(fully1, fw2), fb2))
      fully2 = tf.nn.dropout(fully2, self.keep_place)

      fw3 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb3 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully3 = tf.add(tf.matmul(fully2, fw3), fb3, name='out_put')

      return fully3

    def train(self,data_dir,model_save_path):
      print('ready load train dataset')
      X, y = self.init_data(data_dir)
      # print("===")
      # print(X)
      # print(y)
      # print("===")
      print('success load ' + str(len(y)) + ' datas')
      train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.2, random_state=0)

      out_put = self.cnn_construct()
      predicts = tf.nn.softmax(out_put)
      predicts = tf.argmax(predicts, axis=1)
      actual_y = tf.argmax(self.y_place, axis=1)
      accuracy = tf.reduce_mean(tf.cast(tf.equal(predicts, actual_y), dtype=tf.float32))
      cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out_put, labels=self.y_place))
      opt = tf.train.AdamOptimizer(self.learn_rate)
      train_step = opt.minimize(cost)

      with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            step = 0
            saver = tf.train.Saver()
            while True:
                train_index = np.random.choice(len(train_x), self.batch_size, replace=False)
                train_randx = train_x
                train_randy = train_y
                _, loss = sess.run(, feed_dict={self.x_place: train_randx,
                                                                  self.y_place: train_randy, self.keep_place: 0.75})
                step += 1
                print(step, loss)

                if step % 10 == 0:
                  test_index = np.random.choice(len(test_x), self.batch_size, replace=False)
                  test_randx = test_x
                  test_randy = test_y
                  acc = sess.run(accuracy, feed_dict={self.x_place: test_randx,
                                                      self.y_place: test_randy, self.keep_place: 1.0})
                  print('accuracy:' + str(acc))
                  if acc > 0.99 and step > 500:
                        saver.save(sess, model_save_path, global_step=step)
                        break

    def test(self,x_images,model_path):
      out_put = self.cnn_construct()
      predicts = tf.nn.softmax(out_put)
      probabilitys = tf.reduce_max(predicts, reduction_indices=)
      predicts = tf.argmax(predicts, axis=1)
      saver = tf.train.Saver()
      with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, model_path)
            preds, probs = sess.run(, feed_dict={self.x_place: x_images, self.keep_place: 1.0})
      return preds,probs

    def list_all_files(self,root):
      files = []
      list = os.listdir(root)
      for i in range(len(list)):
            element = os.path.join(root, list)
            if os.path.isdir(element):
                files.extend(self.list_all_files(element))
            elif os.path.isfile(element):
                files.append(element)
      return files

    def init_data(self,dir):
      print(dir)
      X = []
      y = []
      if not os.path.exists(dir):
            raise ValueError('没有找到文件夹')
      files = self.list_all_files(dir)
      print()
      labels = for file in files]
      print(labels)
      print(len(labels))

      for i, file in enumerate(files):
            src_img = cv2.imread(file)
            if src_img.ndim != 3:
                continue
            resize_img = cv2.resize(src_img, (136, 36))
            X.append(resize_img)
            y.append([ if labels == 'has' else ])

      X = np.array(X)
      y = np.array(y).reshape(-1, 2)
      return X, y

    def init_testData(self,dir):
      test_X = []
      if not os.path.exists(dir):
            raise ValueError('没有找到文件夹')
      files = self.list_all_files(dir)
      for file in files:
            src_img = cv2.imread(file, cv2.COLOR_BGR2GRAY)
            if src_img.ndim != 3:
                continue
            resize_img = cv2.resize(src_img, (136, 36))
            test_X.append(resize_img)
      test_X = np.array(test_X)
      return test_X


if __name__ == '__main__':
    cur_dir = sys.path
    print(cur_dir)
    data_dir = os.path.join(cur_dir, './carIdentityData/cnn_plate_train')
    print(data_dir)
    test_dir = os.path.join(cur_dir, './carIdentityData/cnn_plate_test')
    train_model_path = os.path.join(cur_dir, './carIdentityData/model/plate_recongnize/model.ckpt')
    model_path = os.path.join(cur_dir,'./carIdentityData/model/plate_recongnize/model.ckpt-510')

    train_flag = 0
    net = plate_cnn_net()

    if train_flag == 1:
      # 训练模型
      net.train(data_dir,train_model_path)
    else:
      # 测试部分
      test_X = net.init_testData(test_dir)
      preds,probs = net.test(test_X,model_path)
      for i in range(len(preds)):
            pred = preds.astype(int)
            prob = probs
            if pred == 1:
                print('plate',prob)
            else:
                print('no',prob)




愿你 发表于 2020-3-28 14:57:18

if src_img.ndim != 3:
AttributeError: 'NoneType' object has no attribute 'ndim'


刚刚提交的时候无法使用代码格式。

为什么会出现错误:if src_img.ndim != 3:
AttributeError: 'NoneType' object has no attribute 'ndim'

明明前一分钟运行的时候还没有这个错误,为什么现在运行就有了。该有的文件夹都有阿
求明白的人指点迷津~

愿你 发表于 2020-3-28 14:58:37

刚刚提交的时候无法使用代码格式。
import sys
import os
import numpy as np
import cv2
import tensorflow as tf

from sklearn.model_selection import train_test_split


class plate_cnn_net:
    def __init__(self):
      self.img_w,self.img_h = 136,36
      self.y_size = 2
      self.batch_size = 100
      self.learn_rate = 0.001

      self.x_place = tf.placeholder(dtype=tf.float32, shape=, name='x_place')
      self.y_place = tf.placeholder(dtype=tf.float32, shape=, name='y_place')
      self.keep_place = tf.placeholder(dtype=tf.float32, name='keep_place')

    def cnn_construct(self):
      x_input = tf.reshape(self.x_place, shape=[-1, self.img_h, self.img_w, 3])

      cw1 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb1 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x_input, filter=cw1, strides=, padding='SAME'), cb1))
      conv1 = tf.nn.max_pool(conv1, ksize=, strides=, padding='SAME')
      conv1 = tf.nn.dropout(conv1, self.keep_place)

      cw2 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb2 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, filter=cw2, strides=, padding='SAME'), cb2))
      conv2 = tf.nn.max_pool(conv2, ksize=, strides=, padding='SAME')
      conv2 = tf.nn.dropout(conv2, self.keep_place)

      cw3 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      cb3 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, filter=cw3, strides=, padding='SAME'), cb3))
      conv3 = tf.nn.max_pool(conv3, ksize=, strides=, padding='SAME')
      conv3 = tf.nn.dropout(conv3, self.keep_place)

      conv_out = tf.reshape(conv3, shape=[-1, 17 * 5 * 128])

      fw1 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb1 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully1 = tf.nn.relu(tf.add(tf.matmul(conv_out, fw1), fb1))
      fully1 = tf.nn.dropout(fully1, self.keep_place)

      fw2 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb2 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully2 = tf.nn.relu(tf.add(tf.matmul(fully1, fw2), fb2))
      fully2 = tf.nn.dropout(fully2, self.keep_place)

      fw3 = tf.Variable(tf.random_normal(shape=, stddev=0.01), dtype=tf.float32)
      fb3 = tf.Variable(tf.random_normal(shape=), dtype=tf.float32)
      fully3 = tf.add(tf.matmul(fully2, fw3), fb3, name='out_put')

      return fully3

    def train(self,data_dir,model_save_path):
      print('ready load train dataset')
      X, y = self.init_data(data_dir)
      # print("===")
      # print(X)
      # print(y)
      # print("===")
      print('success load ' + str(len(y)) + ' datas')
      train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.2, random_state=0)

      out_put = self.cnn_construct()
      predicts = tf.nn.softmax(out_put)
      predicts = tf.argmax(predicts, axis=1)
      actual_y = tf.argmax(self.y_place, axis=1)
      accuracy = tf.reduce_mean(tf.cast(tf.equal(predicts, actual_y), dtype=tf.float32))
      cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out_put, labels=self.y_place))
      opt = tf.train.AdamOptimizer(self.learn_rate)
      train_step = opt.minimize(cost)

      with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            step = 0
            saver = tf.train.Saver()
            while True:
                train_index = np.random.choice(len(train_x), self.batch_size, replace=False)
                train_randx = train_x
                train_randy = train_y
                _, loss = sess.run(, feed_dict={self.x_place: train_randx,
                                                                  self.y_place: train_randy, self.keep_place: 0.75})
                step += 1
                print(step, loss)

                if step % 10 == 0:
                  test_index = np.random.choice(len(test_x), self.batch_size, replace=False)
                  test_randx = test_x
                  test_randy = test_y
                  acc = sess.run(accuracy, feed_dict={self.x_place: test_randx,
                                                      self.y_place: test_randy, self.keep_place: 1.0})
                  print('accuracy:' + str(acc))
                  if acc > 0.99 and step > 500:
                        saver.save(sess, model_save_path, global_step=step)
                        break

    def test(self,x_images,model_path):
      out_put = self.cnn_construct()
      predicts = tf.nn.softmax(out_put)
      probabilitys = tf.reduce_max(predicts, reduction_indices=)
      predicts = tf.argmax(predicts, axis=1)
      saver = tf.train.Saver()
      with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, model_path)
            preds, probs = sess.run(, feed_dict={self.x_place: x_images, self.keep_place: 1.0})
      return preds,probs

    def list_all_files(self,root):
      files = []
      list = os.listdir(root)
      for i in range(len(list)):
            element = os.path.join(root, list)
            if os.path.isdir(element):
                files.extend(self.list_all_files(element))
            elif os.path.isfile(element):
                files.append(element)
      return files

    def init_data(self,dir):
      print(dir)
      X = []
      y = []
      if not os.path.exists(dir):
            raise ValueError('没有找到文件夹')
      files = self.list_all_files(dir)
      print()
      labels = for file in files]
      print(labels)
      print(len(labels))

      for i, file in enumerate(files):
            src_img = cv2.imread(file)
            if src_img.ndim != 3:
                continue
            resize_img = cv2.resize(src_img, (136, 36))
            X.append(resize_img)
            y.append([ if labels == 'has' else ])

      X = np.array(X)
      y = np.array(y).reshape(-1, 2)
      return X, y

    def init_testData(self,dir):
      test_X = []
      if not os.path.exists(dir):
            raise ValueError('没有找到文件夹')
      files = self.list_all_files(dir)
      for file in files:
            src_img = cv2.imread(file, cv2.COLOR_BGR2GRAY)
            if src_img.ndim != 3:
                continue
            resize_img = cv2.resize(src_img, (136, 36))
            test_X.append(resize_img)
      test_X = np.array(test_X)
      return test_X


if __name__ == '__main__':
    cur_dir = sys.path
    print(cur_dir)
    data_dir = os.path.join(cur_dir, './carIdentityData/cnn_plate_train')
    print(data_dir)
    test_dir = os.path.join(cur_dir, './carIdentityData/cnn_plate_test')
    train_model_path = os.path.join(cur_dir, './carIdentityData/model/plate_recongnize/model.ckpt')
    model_path = os.path.join(cur_dir,'./carIdentityData/model/plate_recongnize/model.ckpt-510')

    train_flag = 0
    net = plate_cnn_net()

    if train_flag == 1:
      # 训练模型
      net.train(data_dir,train_model_path)
    else:
      # 测试部分
      test_X = net.init_testData(test_dir)
      preds,probs = net.test(test_X,model_path)
      for i in range(len(preds)):
            pred = preds.astype(int)
            prob = probs
            if pred == 1:
                print('plate',prob)
            else:
                print('no',prob)


为什么会出现错误:if src_img.ndim != 3:
AttributeError: 'NoneType' object has no attribute 'ndim'

明明前一分钟运行的时候还没有这个错误,为什么现在运行就有了。该有的文件夹都有阿
求明白的人指点迷津~

BngThea 发表于 2020-3-28 14:58:34

说明cv2读取的文件不对,检查一下路径

愿你 发表于 2020-3-28 15:27:44

BngThea 发表于 2020-3-28 14:58
说明cv2读取的文件不对,检查一下路径

检查过了奇怪的是 我上一秒都还能执行 下一秒就不能了

BngThea 发表于 2020-3-28 15:31:27

愿你 发表于 2020-3-28 15:27
检查过了奇怪的是 我上一秒都还能执行 下一秒就不能了

你检查一下files变量,是不是有些不是图片

愿你 发表于 2020-3-29 09:55:54

BngThea 发表于 2020-3-28 15:31
你检查一下files变量,是不是有些不是图片

都是图片呀

BngThea 发表于 2020-3-29 09:59:10

愿你 发表于 2020-3-29 09:55
都是图片呀

用try捕捉一下异常,然后打印对应的 src_img

愿你 发表于 2020-3-29 10:22:12

BngThea 发表于 2020-3-29 09:59
用try捕捉一下异常,然后打印对应的 src_img

    def init_testData(self,dir):
      test_X = []
      if not os.path.exists(dir):
            raise ValueError('没有找到文件夹')
      files = self.list_all_files(dir)
      for file in files:
            src_img = cv2.imread(file, cv2.COLOR_BGR2GRAY)
            try:
                if src_img.ndim != 3:
                  continue
            except Exception as ex:
                print("发生异常")
                print(src_img)
            resize_img = cv2.resize(src_img, (136, 36))
            test_X.append(resize_img)
      test_X = np.array(test_X)
      return test_X

报错信息:Traceback (most recent call last):
File "D:/pycharm/djangocode/project6/python_LPR/newcode/plateNeuralNet.py", line 200, in <module>
    test_X = net.init_testData(test_dir)
File "D:/pycharm/djangocode/project6/python_LPR/newcode/plateNeuralNet.py", line 177, in init_testData
    resize_img = cv2.resize(src_img, (136, 36))
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4044: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

发生异常
None

BngThea 发表于 2020-3-29 11:11:59

不是很清楚了吗,此时src_img 是 None
再打印一下此时的file文件,肯定不是图片

愿你 发表于 2020-3-31 21:06:58

BngThea 发表于 2020-3-29 11:11
不是很清楚了吗,此时src_img 是 None
再打印一下此时的file文件,肯定不是图片

可能是因为有几张有了中文 现在可以啦 谢谢~

lzl134514 发表于 2020-11-20 11:34:05

你好,我也出现和你一样的问题,但我测试后发现src_img返回值为空,file返回值为E:\新建文件夹 (4)\CarPlateIdentity-master\code\carIdentityData/cnn_char_train\0\111-4.jpg,这是什么错误啊?
页: [1]
查看完整版本: 为什么出现错误if src_img.ndim != 3: AttributeError: 'NoneType' object has no ...