如何使用张量流对尺寸大于训练样本的图像进行分类

时间:2019-04-29 22:28:03

标签: tensorflow

我想使用训练模型为256 x 256和3通道(R,G,B)的keras模型识别图像尺寸为6950 x 3715和3通道(R,G,B)的图像中的树)。但是,当预测尺寸为(6950 x 3715)的图像时,它会出现错误“检查输入时出错:预期conv2d_input具有4个尺寸,但数组的形状为(25006,17761,3)”。

如何使用已建立的模型预测图像并将这些确定的树导出到shapefile?

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential, model_from_json
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, 
Conv2D, MaxPooling2D
import cv2, glob, os, random
import numpy as np
import pandas as pd

tf.enable_eager_execution()
AUTOTUNE = tf.data.experimental.AUTOTUNE

def read_labeled_list(label_list_file):
 labels =[]
 for label in label_list_file:
     with open(label) as f_input:
         for line in f_input:
             labels.append(int(line.split()[0]))
 return  labels

def load_and_preprocess_image(path):
  image = tf.read_file(path)
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.resize_images(image, [256, 256])
  image /= 255.0  
  return image
all_image_paths=list(glob.glob('C:/LEARN_TENSORFLOW/images/*.jpg'))
all_image_paths = [str(path) for path in all_image_paths] 
path_ds = tf.data.Dataset.from_tensor_slices(all_image_paths)
image_ds = path_ds.map(load_and_preprocess_image, 
num_parallel_calls=AUTOTUNE)
all_image_labels = 
read_labeled_list(glob.glob('C:/LEARN_TENSORFLOW/labels/*.txt'))
label_ds = tf.data.Dataset.from_tensor_slices(tf.cast(all_image_labels, 
tf.int64))
image_label_ds = tf.data.Dataset.zip((image_ds, label_ds))
ds = image_label_ds.shuffle(buffer_size=image_count) 
ds = ds.repeat()
BATCH_SIZE = 32
ds = ds.batch(BATCH_SIZE)
ds = ds.prefetch(buffer_size=AUTOTUNE)
######BUILD THE MODEL: 
model = Sequential()
model.add(Conv2D(32,(3,3), activation = 'relu',input_shape=[256,256,3]))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(64,(3,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

#########COMPILE MODEL: Step2 - COMPILE MODEL
model.compile(optimizer="adam",
          loss='binary_crossentropy',
          metrics=['accuracy'])
len(model.trainable_variables)
model.summary()
steps_per_epoch=tf.ceil(len(all_image_paths)/10).numpy()
model.fit(ds, epochs=1, steps_per_epoch=2)
####PREDICT TEST IMAGE
img_array = cv2.imread('C:/deeplearning/test_stack.jpg')
img_array= np.array(img_array).reshape(-1,6950,3715,3)
img_array = img_array/255.0
predictions=model.predict(img_array)

2 个答案:

答案 0 :(得分:1)

问题似乎是您正在尝试对尺寸不正确的图像进行评估。通常,应该对要评估的图像和要训练的图像进行相同的预处理,因为基本假设是训练集和测试集是从相同的分布中得出的。例如,这给了我一个预测:

g = tf.Graph()
with g.as_default():
    t = load_and_preprocess_image('C:/deeplearning/test_stack.jpg')
    t = tf.reshape(t, [1, 256, 256, 3])  # make single image into a batch of images
    with tf.Session() as sess:
        img_array = sess.run(t)
predictions=model.predict(img_array)

答案 1 :(得分:1)

由于您已经用256 x 256 x 3的图像训练了模型(第一层是Conv2d层,其输入的形状是256 x 256 x 3),所以要预测的图像应该是256 x 256 x 3张图片。您将不得不将图像调整为输入的大小。