Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow Keras图像不工作_Tensorflow_Keras - Fatal编程技术网

Tensorflow Keras图像不工作

Tensorflow Keras图像不工作,tensorflow,keras,Tensorflow,Keras,在Pluralsight上从Jerry Kurata那里学到,我正在尝试识别鸟类: 我的数据集结构是: 我的模型培训代码是: import glob import matplotlib.pyplot as plt from keras import backend as K import tensorflow as tf with K.tf.device("/device:GPU:0"): config = tf.ConfigProto(intra_op_parallelism_th

在Pluralsight上从Jerry Kurata那里学到,我正在尝试识别鸟类:

我的数据集结构是:

我的模型培训代码是:

import glob
import matplotlib.pyplot as plt

from keras import backend as K
import tensorflow as tf
with K.tf.device("/device:GPU:0"):
    config = tf.ConfigProto(intra_op_parallelism_threads=4,
           inter_op_parallelism_threads=4, allow_soft_placement=True,
           device_count = {'CPU' : 1, 'GPU' : 1})
    session = tf.Session(config=config)
    K.set_session(session)

from keras.callbacks import EarlyStopping
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
# "/device:GPU:0"
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

def get_num_files(path):
    if not os.path.exists(path):
        return 0
    return sum([len(files) for r, d, files in os.walk(path)])

def get_num_subfolders(path):
    if not os.path.exists(path):
        return 0
    return sum([len(d) for r, d, files in os.walk(path)])

def create_img_generator():
    return ImageDataGenerator(
        preprocessing_function=preprocess_input,
        rotation_range=30,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True
    )

Image_width, Image_height = 299, 299
Training_Epochs = 1
Batch_Size = 32
Number_FC_Neurons = 1024

train_dir = '.../birds/train'
validate_dir = '.../birds/validation'

num_train_samples = get_num_files(train_dir)
num_classes = get_num_subfolders(train_dir)
num_validate_samples = get_num_files(validate_dir)

num_epoch = Training_Epochs
batch_size = Batch_Size

train_image_gen = create_img_generator()
test_image_gen = create_img_generator()

train_generator = train_image_gen.flow_from_directory(
    train_dir,
    target_size=(Image_width, Image_height),
    batch_size = batch_size,
    seed = 42
)

validation_generator = test_image_gen.flow_from_directory(
    validate_dir,
    target_size=(Image_width, Image_height),
    batch_size=batch_size,
    seed=42
)

Inceptionv3_model = InceptionV3(weights='imagenet', include_top=False)
print('Inception v3 model without last FC loaded')

x = Inceptionv3_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(Number_FC_Neurons, activation='relu')(x)
predictions = Dense(num_classes, activation='softmax')(x)

model = Model(inputs=Inceptionv3_model.input, outputs=predictions)
print(model.summary())

print('\nFine tuning existing model')

Layers_To_Freeze = 172
for layer in model.layers[:Layers_To_Freeze]:
    layer.trainable = False
for layer in model.layers[Layers_To_Freeze:]:
    layer.trainable = True

model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='binary_crossentropy', metrics=['accuracy'])

cbk_early_stopping = EarlyStopping(monitor='val_acc', mode='max')

history_transfer_learning = model.fit_generator(
    train_generator,
    steps_per_epoch = num_train_samples,
    epochs=num_epoch,
    validation_data=validation_generator,
    validation_steps = num_validate_samples,
    class_weight='auto',
    callbacks=[cbk_early_stopping]
)

model.save('incepv3_transfer.h5', overwrite=True, include_optimizer=True)
我的探测器是

from keras.models import load_model
from keras.optimizers import SGD
from keras.preprocessing import image
from keras.applications.inception_v3 import preprocess_input
import matplotlib.pyplot as plt
import numpy as np
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

class Detector:
    def __init__(self, model_path):
        self.model = load_model(model_path)
        print('input shape')   # output is always (None, None, None, 3), this should be wrong
        print(self.model.layers[0].input_shape)
        # self.model.summary()
        # self.model.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.0001, momentum=0.9), metrics=['accuracy'])

    def preprocess_input(self, x):
        y = np.copy(x)
        y /= 255.
        y -= 0.5
        y *= 2.
        return y

    def load_image(self, img_path, show=False):
        img = image.load_img(img_path, target_size=(299,299))
        img_tensor = image.img_to_array(img)                    # (height, width, channels)
        img_tensor = np.expand_dims(img, axis=0)         # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
        # img_tensor /= 255.                                      # imshow expects values in the range [0, 1]
        img_tensor = preprocess_input(img_tensor)

        if show:
            plt.imshow(img_tensor[0])
            plt.axis('off')
            plt.show()

        return img_tensor

    def detect(self, img_path):
        img = self.load_image(img_path, True)
        classes = self.model.predict(img)
        return classes

下面是我如何使用它们来预测图像中是否有鸟:

from keras.models import Model
from detector import Detector
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

model_path = 'incepv3_transfer.h5'
detective = Detector(model_path)

bird_img = 'b1.jpeg'
classes = detective.detect(bird_img)

print(classes)


bird_img = 'dog1.jpg'
classes = detective.detect(bird_img)

print(classes)
输出总是:

[[1.]]


我不太确定你想做什么。你只想知道照片上有没有鸟吗?在
flow\u from\u目录中
您需要一个包含每个类的图像的文件夹,您是否提供了这些文件夹?我认为你需要两者,鸟的形象和没有鸟的形象。如果你只在一门课上训练,网络将只学习产生一个独立于其输入的1。你在训练中的失利情况如何?验证丢失是什么样子的?如果所提供的类出现问题,它们都应该始终保持在0。嗨@Solvalou我想知道图像中是否有鸟。我已更新代码以包含我的数据集文件夹结构。我只想探测鸟类,不需要探测猫或狗。我觉得我需要在train和validation文件夹下提供一个名为non birds的文件夹???但是当我使用二进制交叉熵时,我还需要提供非鸟类图像吗?什么结构?你好,索尔瓦鲁你的问题很有帮助!我记得我看到的准确度总是1.00,损失是1.xxx e-7。是的,我实际上认为你需要提供非鸟类的图像。通过这样做,您最终将得到两个输出类别。但这没有多大意义,因为两个输出都是高度相关的。因此,要再次获得单个输出,您可以将图像生成器包装到另一个生成器中。在这里,您将获得输入生成器的下一个输出,它将是一个图像及其所属的类别。然后生成一个元组,再次由图像和一个新编码的类组成。因此,如果没有bird,则新类将为0,如果有bird,则新类将为1。因为此时您只处理一个输出,所以您应该将最终密集层中的激活从softmax更改为sigmoid。这是因为softmax用于多个类,是sigmoid的推广。我也不确定这是否有效,我只是在自言自语。如果我有时间的话,也许我可以自己试试。