我正在尝试使用以下代码从以下链接中训练Keras-I3D模型:

https://github.com/srijandas07/i3d

导入的模块是

import os
os.environ['KERAS_BACKEND'] = 'tensorflow'

os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="3"

from keras.layers import Dense, Flatten, Dropout, Reshape
from keras import regularizers
from keras.preprocessing import image
from keras.models import Model, load_model
from keras.applications.vgg16 import preprocess_input
from keras.utils import to_categorical
from keras.optimizers import SGD
from i3d_inception import Inception_Inflated3d, conv3d_bn
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, CSVLogger, Callback
from keras.utils import Sequence, multi_gpu_model

import random
import sys
from multiprocessing import cpu_count
import numpy as np
import glob
from skimage.io import imread
import cv2


一些定义

epochs = str(sys.argv[0])
#epochs = 17
model_name = sys.argv[0]
#model_name = model_name
version = sys.argv[0]
num_classes = 35
batch_size = 16
stack_size = 64
DataLoader_video_train = DataLoader_video_train
DataLoader_video_test = DataLoader_video_test


class CustomModelCheckpoint(Callback):

    def __init__(self, model_parallel, path):

        super(CustomModelCheckpoint, self).__init__()

        self.save_model = model_parallel
        self.path = path
        self.nb_epoch = 0

    def on_epoch_end(self, epoch, logs=None):
        self.nb_epoch += 1
        self.save_model.save(self.path + str(self.nb_epoch) + '.hdf5')


i3d = i3d_modified(weights = 'rgb_imagenet_and_kinetics')
model = i3d.i3d_flattened(num_classes = num_classes)
optim = SGD(lr = 0.01, momentum = 0.9)



csvlogger这里存在问题

reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor = 0.1, patience = 10)
csvlogger = CSVLogger('i3d_'+model_name+'.csv')



model.compile(loss = 'categorical_crossentropy', optimizer = optim, metrics = ['accuracy'])

model_checkpoint = CustomModelCheckpoint(model, './weights_'+model_name+'/epoch_')

train_generator = DataLoader_video_train('/train_CS.txt',version, batch_size = batch_size)
test_generator = DataLoader_video_test('/test_CS.txt', version, batch_size = batch_size)


适合发电机

model.fit_generator(
    generator = train_generator,
    #validation_data=val_generator,
    epochs = epochs,
    steps_per_epoch = 17,
    callbacks = [csvlogger, reduce_lr, model_checkpoint],
    max_queue_size = 48,
    workers = cpu_count() - 2,
    use_multiprocessing = True,
)

print(model.evaluate_generator(generator = test_generator))


我收到以下错误

runfile('D:/Clones/i3d-master/i3d_train.py', wdir='D:/Clones/i3d-master')
Reloaded modules: i3d_inception
C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training_generator.py:47: UserWarning: Using a generator with `use_multiprocessing=True` and multiple workers may duplicate your data. Please consider using the`keras.utils.Sequence class.
  UserWarning('Using a generator with `use_multiprocessing=True`'
Traceback (most recent call last):

  File "<ipython-input-30-8f7b9cc152d8>", line 1, in <module>
    runfile('D:/Clones/i3d-master/i3d_train.py', wdir='D:/Clones/i3d-master')

  File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Clones/i3d-master/i3d_train.py", line 109, in <module>
    use_multiprocessing = True,

  File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)

  File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 102, in fit_generator
    callbacks.on_train_begin()

  File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\callbacks.py", line 132, in on_train_begin
    callback.on_train_begin(logs)

  File "C:\Users\sancy\Anaconda3\lib\site-packages\keras\callbacks.py", line 1183, in on_train_begin
    **self._open_args)

OSError: [Errno 22] Invalid argument: 'i3d_D:/Clones/i3d-master/i3d_train.py.csv'


我究竟做错了什么?如何正确使用str(sys.argv [0])和CSVLogger?

最佳答案

文件名中的'i3d_'+似乎导致csvlogger = CSVLogger('i3d_'+model_name+'.csv')中的文件名无效。尝试删除i3d_前缀。

07-24 09:52