Pythonで自分の作成した学習データで物体検出を行おうと思い、コードが公開されていたssd_kerasを用いました。
(参考にしたサイト:①https://karaage.hatenadiary.jp/entry/2018/06/04/070000及び②https://github.com/karaage0703/ssd_keras)
keras,tensorflowのバージョンを合わせてコードを実行してみた所、モデルが学習している最中にエラーメッセージが発生しました。
発生している問題
参考にしたサイト①で紹介されている方法で学習用のデータを作製したのち
参考にしたサイト②のSSD_train_custom.pyを実行したのですが、
(以下SSD_train_custom.pyのコード
... def generate(self, train=True): while True: if train: shuffle(self.train_keys) keys = self.train_keys else: shuffle(self.val_keys) keys = self.val_keys inputs = [] targets = [] for key in keys: img_path = self.path_prefix + key + ".jpg" img = imread(img_path).astype('float32') y = self.gt[key].copy() if train and self.do_crop: img, y = self.random_sized_crop(img, y) print(img.shape) img = imresize(img, self.image_size).astype('float32') print("shape",img.shape) if train: shuffle(self.color_jitter) for jitter in self.color_jitter: img = jitter(img) if self.lighting_std: img = self.lighting(img) if self.hflip_prob > 0: img, y = self.horizontal_flip(img, y) if self.vflip_prob > 0: img, y = self.vertical_flip(img, y) y = self.bbox_util.assign_boxes(y) inputs.append(img) targets.append(y) if len(targets) == self.batch_size: tmp_inp = np.array(inputs) tmp_targets = np.array(targets) inputs = [] targets = [] yield preprocess_input(tmp_inp), tmp_targets checkpoints_dir = r"C:\Users\User\ssd_keras\checkpoints" path_prefix = './PASCAL_VOC/data/20180929_output/JPEGImages/' gen = Generator(gt, bbox_util, 4, path_prefix, train_keys, val_keys, (input_shape[0], input_shape[1]), do_crop=False) model = SSD300(input_shape, num_classes=NUM_CLASSES) model.load_weights('weights_SSD300.hdf5', by_name=True) freeze = ['input_1', 'conv1_1', 'conv1_2', 'pool1', 'conv2_1', 'conv2_2', 'pool2', 'conv3_1', 'conv3_2', 'conv3_3', 'pool3']#, for L in model.layers: if L.name in freeze: L.trainable = False def schedule(epoch, decay=0.9): return base_lr * decay**(epoch) callbacks = [keras.callbacks.ModelCheckpoint('./checkpoints/weights.{epoch:02d}-{val_loss:.2f}.hdf5', verbose=1, save_weights_only=True), keras.callbacks.LearningRateScheduler(schedule)] base_lr = 3e-4 optim = keras.optimizers.Adam(lr=base_lr) model.compile(optimizer=optim, loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=2.0).compute_loss, metrics=['accuracy']) nb_epoch = 100 start = time.time() print("##########") print("gen.generate(True)",gen.generate(True)) print("gen.train_batches",gen.train_batches) print("callbacks",callbacks) print("gen.generate(False)",gen.generate(False)) print("gen.val_batches",gen.val_batches) history = model.fit_generator(gen.generate(True), gen.train_batches, nb_epoch, verbose=1, callbacks=callbacks, validation_data=gen.generate(False), nb_val_samples=gen.val_batches, nb_worker=1) print("##########") elapsed_time = time.time() - start print ("elapsed_time:{0}".format(elapsed_time) + "[sec]") ....
)
以下のようなエラーが出てしまいました。
Exception in thread Thread-9:
Traceback (most recent call last):
File "C:\Users\User\Anaconda3\envs\ssd1\lib\threading.py", line 914, in _bootstrap_inner
self.run()
File "C:\Users\User\Anaconda3\envs\ssd1\lib\threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\User\Anaconda3\envs\ssd1\lib\site-packages\keras\engine\training.py", line 429, in data_generator_task
generator_output = next(self._generator)
File "<ipython-input-4-d40b5c4c61c2>", line 211, in generate
img, y = self.vertical_flip(img, y)
File "<ipython-input-4-d40b5c4c61c2>", line 124, in vertical_flip
y[:, [1, 3]] = 1 - y[:, [3, 1]]
IndexError: too many indices for array
ValueError Traceback (most recent call last)
<ipython-input-4-d40b5c4c61c2> in <module>()
273 validation_data=gen.generate(False),
274 nb_val_samples=gen.val_batches,
--> 275 nb_worker=1)
276 print("##########")
277 elapsed_time = time.time() - start
~\Anaconda3\envs\ssd1\lib\site-packages\keras\engine\training.py in fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe, initial_epoch)
1530 '(x, y, sample_weight) '
1531 'or (x, y). Found: ' +
-> 1532 str(generator_output))
1533 if len(generator_output) == 2:
1534 x, y = generator_output
ValueError: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None
参考にしたサイト②のSSD_training.ipynbも見てみましたが同様のエラーが出ているようで解決には至りませんでした。
どう直せばよいのでしょうか。
補足情報(FW/ツールのバージョンなど)
keras v1.2.2
tensorflow v1.0.0
scipy v0.19.1
回答2件
あなたの回答
tips
プレビュー