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



我想使用带有大小为 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)

看起来问题是您正在尝试评估大小不正确的图像。通常,您应该对评估的图像应用与训练图像相同的预处理,因为基本假设是训练集和测试集是从同一分布中提取的。例如,这给了我一个预测:

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)
由于您已使用 256 x 256 x 3

图像(第一层是 Conv2d 层,其输入的形状为 256 x 256 x 3(训练模型,因此要预测的图像应该是 256 x 256 x 3 的图像。您必须将图像调整为输入大小。

最新更新