首页 > 解决方案 > 如何使用张量流对尺寸大于训练样本的图像进行分类

问题描述

我想使用 keras 模型识别图像大小为 6950 x 3715 和 3 个通道(R、G、B)的图像中的树木,训练图像的大小为 256 x 256 和 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)

标签: tensorflow

解决方案


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

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)

推荐阅读