def read_copy(): if not os.path.exists(os.path.join(os.path.pardir, 'OUTPUT')): os.mkdir(os.path.join(os.path.pardir, 'OUTPUT')) for i in range(10): # 检查输出文件夹是否存在 if not os.path.exists(os.path.join(os.path.pardir, 'OUTPUT', str(i))): os.mkdir(os.path.join(os.path.pardir, 'OUTPUT', str(i))) dataset = tf.data.TFRecordDataset(RECORD_NAME) dataset = dataset.map(read_decode) dataset = dataset.repeat(1) dataset = dataset.batch(5) iterator = dataset.make_one_shot_iterator() batch_image, batch_name, batch_label = iterator.get_next() score_label = model(batch_image, False) pred_label = tf.argmax(score_label, 1) softmax = tf.nn.softmax(score_label) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() y_true, y_pred, names, label_prob, label_score = [], [], [], [], [] with tf.Session() as sess: sess.run(init_op) saver.restore(sess, os.path.join(os.path.pardir, 'model', 'model1.ckpt')) while True: try: label_pred, name, label_true, pred_prob, pred_score = sess.run( [ pred_label, batch_name, batch_label, softmax, score_label ]) # name = str(name[0],encoding='utf8') print(label_pred, label_true) y_true.extend(label_true) y_pred.extend(label_pred) names.extend(name) label_prob.extend(pred_prob) label_score.extend(pred_score) # n = os.path.split(name) # shutil.copyfile(name,os.path.join(os.path.pardir,'OUTPUT',str(label), '%s_%s'%(n[0][3:], n[1]))) except: print('Finish') break return y_true, y_pred, names, label_prob, label_score
def read_copy(folder_type): if not os.path.exists(PATH_OUT): os.mkdir(PATH_OUT) if not os.path.exists(os.path.join(PATH_OUT, folder_type)): os.mkdir(os.path.join(PATH_OUT, folder_type)) for i in range(9): # 检查输出文件夹是否存在 if not os.path.exists(os.path.join(PATH_OUT, folder_type, str(i))): os.mkdir(os.path.join(PATH_OUT, folder_type, str(i))) dataset = tf.data.TFRecordDataset(RECORD_NAME) dataset = dataset.map(read_decode) dataset = dataset.repeat(1) dataset = dataset.batch(5) iterator = dataset.make_one_shot_iterator() batch_image, batch_name = iterator.get_next() batch_score = model(batch_image, False) batch_pred = tf.argmax(batch_score, 1) batch_softmax = tf.nn.softmax(batch_score) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() names, scores, preds, softmaxs = [], [], [], [] with tf.Session() as sess: sess.run(init_op) saver.restore(sess, os.path.join(os.path.pardir, 'model', 'model1.ckpt')) while True: try: name, pred, score, softmax = sess.run( [batch_name, batch_pred, batch_score, batch_softmax]) name = list(map(lambda x: str(x, encoding='utf8'), name)) print(pred) for p, n in zip(pred, name): # n = os.path.split(name) shutil.copyfile( n, os.path.join(PATH_OUT, folder_type, str(p), os.path.split(n)[1])) except Exception as e: print('Finish') # print(e) break
def read_copy(): if not os.path.exists(os.path.join(PATH_OUT)): os.mkdir(os.path.join(PATH_OUT)) for i in range(10): # 检查输出文件夹是否存在 if not os.path.exists(os.path.join(PATH_OUT, str(i))): os.mkdir(os.path.join(PATH_OUT, str(i))) dataset = tf.data.TFRecordDataset(RECORD_NAME) dataset = dataset.map(read_decode) dataset = dataset.repeat(1) dataset = dataset.batch(5) iterator = dataset.make_one_shot_iterator() batch_image, batch_name = iterator.get_next() score_label = model(batch_image, False) pred_label = tf.argmax(score_label, 1) softmax = tf.nn.softmax(score_label) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() y_true, y_pred, names, label_prob, label_score = [], [], [], [], [] with tf.Session() as sess: sess.run(init_op) saver.restore(sess, os.path.join(PATH_MODEL, 'model1.ckpt')) while True: try: label_pred, name = sess.run([pred_label, batch_name]) name = list(map(decode_name, name)) # print(label_pred, name) for p, n in zip(label_pred, name): print(p, n) out_n = os.path.join( PATH_OUT, str(p), '%s_%s' % (os.path.split(n)[0][-29:], os.path.split(n)[1])) shutil.copyfile(n, out_n) except: print('Finish') break
def read_copy(): dataset = tf.data.TFRecordDataset(RECORD_NAME) dataset = dataset.map(read_decode) dataset = dataset.repeat(1) dataset = dataset.batch(5) iterator = dataset.make_one_shot_iterator() batch_image, batch_name = iterator.get_next() score_label = model(batch_image, False) pred_label = tf.argmax(score_label, 1) softmax = tf.nn.softmax(score_label) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() y_true, y_pred, names, label_prob, label_score = [], [], [], [], [] with tf.Session() as sess: sess.run(init_op) saver.restore(sess, os.path.join(PATH_MODEL, 'model1.ckpt')) while True: try: label_pred, name, label_prob = sess.run( [pred_label, batch_name, softmax]) name = list(map(decode_name, name)) # print(label_pred, name) label_prob = np.array(label_prob) print(label_prob.shape) for p, n, s in zip(label_pred, name, label_prob.max(axis=1)): if s < 0.95: # 如果准确率低于阈值,则移除掉该图片 print(p, n, s) out_n = os.path.join(PATH_OUT, 'other', os.path.split(n)[1]) shutil.copyfile(n, out_n) # 复制到其他文件夹 os.remove(n) # 删除图片 except Exception as e: print('Finish') # print(e) break
def read_copy(folder,IDX_5, IDX_7): dataset = tf.data.TFRecordDataset(RECORD_NAME) dataset = dataset.map(read_decode) dataset = dataset.repeat(1) dataset = dataset.batch(5) iterator = dataset.make_one_shot_iterator() batch_image, batch_name = iterator.get_next() score_label = model(batch_image, False) pred_label = tf.argmax(score_label, 1) softmax = tf.nn.softmax(score_label) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) saver = tf.train.Saver() y_true, y_pred, names, label_prob, label_score = [], [], [], [], [] with tf.Session() as sess: sess.run(init_op) saver.restore(sess, os.path.join(PATH_MODEL,'model3.ckpt')) while True: try: label_pred,name,label_prob = sess.run([pred_label, batch_name,softmax]) name = list(map(decode_name, name)) # print(label_pred, name) label_prob = np.array(label_prob) # print(label_prob.shape) for p,n,s in zip(label_pred, name,label_prob.max(axis=1)): # if s < 0.95: print(p,n,s) if p == 0: img = PImage.open(n) img = img.transpose(PImage.ROTATE_270) if p == 2: img = PImage.open(n) img = img.transpose(PImage.ROTATE_90) if folder == '5': h = img.size[0] w = img.size[1] img = img.crop((int(w/6), int(h/3), int(w*0.5), int(h*0.78))).resize((500, 500)) out_n = os.path.join(PATH_OUT,folder + '_rotate',"%06d.jpg" % (IDX_5+300)) IDX_5+=1 if folder == '7': w = img.size[0] h = img.size[1] img = img.crop((int(w*0.5), int(h*0.3), int(w*0.95), int(h*0.8))) img = img.resize((500, 500)) out_n = os.path.join(PATH_OUT,folder + '_rotate',"%06d.jpg" % (IDX_7+10000)) IDX_7 +=1 img.save(out_n) # n = os.path.split(name) # out_n = os.path.join(PATH_OUT,'other',os.path.split(n)[1]) # shutil.copyfile(n,out_n) # os.remove(n) except Exception as e: print('Finish') print(e) break return IDX_5, IDX_7