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

问题描述 投票:0回答:2

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

由于您已经使用256 x 256 x 3图像训练模型(第一层是Conv2d层,其输入的形状为256 x 256 x 3),因此要预测的图像应为256 x 256 x 3图像。您必须将图像重塑为输入尺寸。

© www.soinside.com 2019 - 2024. All rights reserved.