KeyError when trying to fine tuning Bert for text classification - pytorch

I am trying to fine tune Bert for text classification on my dataset and I am getting the following error:
KeyError: 'Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers'
Here is the full error:
1/1 * Epoch (train): 0% 0/613 [00:00<?, ?it/s]Traceback (most recent call last):
File "train.py", line 47, in <module>
runner.train(
File "/usr/local/lib/python3.8/dist-packages/catalyst/runners/runner.py", line 377, in train
self.run()
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 422, in run
self._run_event("on_exception")
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 365, in _run_event
getattr(self, event)(self)
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 357, in on_exception
raise self.exception
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 419, in run
self._run()
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 410, in _run
self.engine.spawn(self._run_local)
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/engine.py", line 59, in spawn
return fn(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 405, in _run_local
self._run_experiment()
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 399, in _run_experiment
self._run_epoch()
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 391, in _run_epoch
self._run_loader()
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 384, in _run_loader
self._run_event("on_batch_start")
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 361, in _run_event
getattr(self, event)(self)
File "/usr/local/lib/python3.8/dist-packages/catalyst/runners/supervised.py", line 150, in on_batch_start
super().on_batch_start(runner)
File "/usr/local/lib/python3.8/dist-packages/catalyst/core/runner.py", line 321, in on_batch_start
self.batch_size = len(self.batch[0])
File "/usr/local/lib/python3.8/dist-packages/transformers/tokenization_utils_base.py", line 241, in __getitem__
raise KeyError(
KeyError: 'Indexing with integers (to access backend Encoding for a given batch index) is not available when using Python based tokenizers'
The code I am using for data preparation:
import logging
from pathlib import Path
from typing import List, Mapping, Tuple
import pandas as pd
import torch
from catalyst.utils import set_global_seed
from torch.utils.data import DataLoader, Dataset
from transformers import AutoTokenizer
class TextClassificationDataset(Dataset):
"""
Wrapper around Torch Dataset to perform text classification
"""
def __init__(
self,
texts: List[str],
labels: List[str] = None,
label_dict: Mapping[str, int] = None,
max_seq_length: int = 512,
model_name: str = "GroNLP/hateBERT",
):
"""
Args:
texts (List[str]): a list with texts to classify or to train the
classifier on
labels List[str]: a list with classification labels (optional)
label_dict (dict): a dictionary mapping class names to class ids,
to be passed to the validation data (optional)
max_seq_length (int): maximal sequence length in tokens,
texts will be stripped to this length
model_name (str): transformer model name, needed to perform
appropriate tokenization
"""
self.texts = texts
self.labels = labels
self.label_dict = label_dict
self.max_seq_length = max_seq_length
if self.label_dict is None and labels is not None:
# {'class1': 0, 'class2': 1, 'class3': 2, ...}
# no easily handle unknown target values
self.label_dict = dict(zip(sorted(set(labels)), range(len(set(labels)))))
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
# suppresses tokenizer warnings
logging.getLogger("transformers.tokenization_utils").setLevel(logging.FATAL)
def __len__(self) -> int:
"""
Returns:
int: length of the dataset
"""
return len(self.texts)
def __getitem__(self, index) -> Mapping[str, torch.Tensor]:
"""Gets element of the dataset
Args:
index (int): index of the element in the dataset
Returns:
Single element by index
"""
# encoding the text
x = self.texts[index]
# a dictionary with `input_ids` and `attention_mask` as keys
output_dict = self.tokenizer.encode_plus(
x,
add_special_tokens=True,
padding="max_length",
max_length=self.max_seq_length,
return_tensors="pt",
truncation=True,
return_attention_mask=True,
)
# for Catalyst, there needs to be a key called features
output_dict["features"] = output_dict["input_ids"].squeeze(0)
del output_dict["input_ids"]
# encoding target
if self.labels is not None:
y = self.labels[index]
y_encoded = torch.Tensor([self.label_dict.get(y, -1)]).long().squeeze(0)
output_dict["targets"] = y_encoded
return output_dict
What is the problem?
I know questions about similar error have been already asked but they were not of much help in solving this problem.

Related

Onnxruntime conversion error from Pytorch model to onnx

I have the follow code to convert from pytorch model to onnx.
#Function to Convert to ONNX
import argparse
import io
import numpy as np
from torch import nn
import torch.utils.model_zoo as model_zoo
import torch.onnx
from torchvision import models
from mmcv.runner import get_dist_info, init_dist, load_checkpoint
from mmaction.models import build_model
from mmcv import Config
from mmaction.utils import (build_ddp, build_dp, default_device)
def parse_args():
parser = argparse.ArgumentParser(description='MMAction2')
parser.add_argument(
'--conf',
default='/workspace/mmaction2/work_dirs/slowfast_kinetics_pretrained_r50_8x8x1_cosine_10e_ava22_rgb/slowfast_kinetics_pretrained_r50_8x8x1_cosine_10e_ava22_rgb.py',
help='This is config file')
parser.add_argument(
'--checkpoint',
default='/workspace/mmaction2/work_dirs/slowfast_kinetics_pretrained_r50_8x8x1_cosine_10e_ava22_rgb/latest.pth',
help='This is checkpoint file')
args = parser.parse_args()
return args
def turn_off_pretrained(cfg):
# recursively find all pretrained in the model config,
# and set them None to avoid redundant pretrain steps for testing
if 'pretrained' in cfg:
cfg.pretrained = None
# recursively turn off pretrained value
for sub_cfg in cfg.values():
if isinstance(sub_cfg, dict):
turn_off_pretrained(sub_cfg)
def convert(args):
cfg = Config.fromfile(args.conf)
turn_off_pretrained(cfg.model)
# build the model and load checkpoint
model = build_model(cfg.model, train_cfg=None, test_cfg=cfg.get('test_cfg'))
load_checkpoint(model, args.checkpoint, map_location='cpu')
model = build_dp(model, default_device, default_args=dict(device_ids=cfg.gpu_ids))
# set the model to inference mode
model.eval()
#device = torch.device("cpu" if args.cpu else "cuda")
#model = model.to(device)
# Let's create a dummy input tensor
dummy_input = torch.randn(1, 3, 1080, 1920).to(default_device)
# Export the model
torch.onnx.export(model.module, # model being run
dummy_input, # model input (or a tuple for multiple inputs)
"slow_fast_ava.onnx", # where to save the model
export_params=True, # store the trained parameter weights inside the model file
opset_version=11, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
print(" ")
print('Model has been converted to ONNX')
if __name__ == '__main__':
args = parse_args()
convert(args )
Pytorch model is attached here.
The error in conversion is
File "mmaction2/demo/convert_to_onnx_1.py", line 57, in convert
torch.onnx.export(model.module, # model being run
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/__init__.py", line 350, in export
return utils.export(
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 163, in export
_export(
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 1074, in _export
graph, params_dict, torch_out = _model_to_graph(
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 727, in _model_to_graph
graph, params, torch_out, module = _create_jit_graph(model, args)
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 602, in _create_jit_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args)
File "/usr/local/lib/python3.8/dist-packages/torch/onnx/utils.py", line 517, in _trace_and_get_graph_from_model
trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 1175, in _get_trace_graph
outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
return forward_call(*input, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 127, in forward
graph, out = torch._C._create_graph_by_tracing(
File "/usr/local/lib/python3.8/dist-packages/torch/jit/_trace.py", line 118, in wrapper
outs.append(self.inner(*trace_inputs))
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1130, in _call_impl
return forward_call(*input, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1118, in _slow_forward
result = self.forward(*input, **kwargs)
File "/workspace/mmcv/mmcv/runner/fp16_utils.py", line 116, in new_func
return old_func(*args, **kwargs)
File "/workspace/mmdetection/mmdet/models/detectors/base.py", line 168, in forward
assert len(img_metas) == 1
File "/usr/local/lib/python3.8/dist-packages/torch/_tensor.py", line 705, in __len__
raise TypeError("len() of a 0-d tensor")
TypeError: len() of a 0-d tensor

pytorch: NotImplementedError when trying to iterate a dataloader

I am trying to create a custom IterableDataset in pytorch and split it into train, validation and test datasets using this answer https://stackoverflow.com/a/61818182/9478434 .
My dataset class is:
class EchoDataset(torch.utils.data.IterableDataset):
def __init__(self, delay=4, seq_length=15, size=1000):
super(EchoDataset).__init__()
self.delay = delay
self.seq_length = seq_length
self.size = size
def __len__(self):
return self.size
def __iter__(self):
""" Iterable dataset doesn't have to implement __getitem__.
Instead, we only need to implement __iter__ to return
an iterator (or generator).
"""
for _ in range(self.size):
seq = torch.tensor([random.choice(range(1, N + 1)) for i in range(self.seq_length)], dtype=torch.int64)
result = torch.cat((torch.zeros(self.delay), seq[:self.seq_length - self.delay])).type(torch.int64)
yield seq, result
And the dataset is created and splitted as:
DELAY = 4
DATASET_SIZE = 200000
ds = EchoDataset(delay=DELAY, size=DATASET_SIZE)
train_count = int(0.7 * DATASET_SIZE)
valid_count = int(0.2 * DATASET_SIZE)
test_count = DATASET_SIZE - train_count - valid_count
train_dataset, valid_dataset, test_dataset = torch.utils.data.random_split(
ds, (train_count, valid_count, test_count)
)
The problem is that when I want to iterate into the dataloader, I get NotImplementedError:
iterator = iter(train_dataset_loader)
print(next(iterator))
I get:
NotImplementedError: Caught NotImplementedError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "venv/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
data = fetcher.fetch(index)
File "venv/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 49, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "venv/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 49, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "venv/lib/python3.6/site-packages/torch/utils/data/dataset.py", line 363, in __getitem__
return self.dataset[self.indices[idx]]
File "venv/lib/python3.6/site-packages/torch/utils/data/dataset.py", line 69, in __getitem__
raise NotImplementedError
NotImplementedError
It seems the problem goes back to splitting the dataset and creating Subset objects since I can iterate through a Dataloader created from the original dataset (not splitted)

Copy paste a Naive Bayes example code on vscode but got errors

I copied the code from datacamp to try the Naive Bayes classification on my own on python 3.8 . but when run the code the compiler gives this error
Traceback (most recent call last):
File "c:\Users\USER\Desktop\DATA MINING\NaiveTest.py", line 34, in <module>
model.fit(features,label)
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\naive_bayes.py", line 207, in fit
X, y = self._validate_data(X, y)
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\base.py", line 433, in _validate_data
X, y = check_X_y(X, y, **check_params)
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 814, in check_X_y
X = check_array(X, accept_sparse=accept_sparse,
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 630, in check_array
raise ValueError(
ValueError: Expected 2D array, got scalar array instead:
array=<zip object at 0x0F2C4C28>.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
I am posting the whole code cause I'm not sure which part that causes this so I'm requesting help to solve this.
# Assigning features and label variables
weather=['Sunny','Sunny','Overcast','Rainy','Rainy','Rainy','Overcast','Sunny','Sunny','Rainy','Sunny','Overcast','Overcast','Rainy']
temp=['Hot','Hot','Hot','Mild','Cool','Cool','Cool','Mild','Cool','Mild','Mild','Mild','Hot','Mild']
play=['No','No','Yes','Yes','Yes','No','Yes','No','Yes','Yes','Yes','Yes','Yes','No']
# Import LabelEncoder
from sklearn import preprocessing
#creating labelEncoder
le = preprocessing.LabelEncoder()
# Converting string labels into numbers.
weather_encoded=le.fit_transform(weather)
print (weather_encoded)
temp_encoded=le.fit_transform(temp)
label=le.fit_transform(play)
print ("Temp:",temp_encoded)
print ("Play:",label)
#Combinig weather and temp into single listof tuples
features=zip(weather_encoded,temp_encoded)
print(list(zip(weather_encoded,temp_encoded)))
print([i for i in zip(weather_encoded,temp_encoded)])
from sklearn.naive_bayes import GaussianNB
#Create a Gaussian Classifier
model = GaussianNB()
# Train the model using the training sets
model.fit(features,label)
#Predict Output
predicted= model.predict([[0,2]]) # 0:Overcast, 2:Mild
print ("Predicted Value:", predicted)
supposedly the result something like this Predicted Value: [1]
but it gave this error instead
What happens is that features should be a list to be passed to model.fit, currently they are type zip
#Combinig weather and temp into single listof tuples
features=zip(weather_encoded,temp_encoded)
you may need to convert features to list, e.g.
#Combinig weather and temp into single listof tuples
features=list(zip(weather_encoded,temp_encoded))

Why can't I fit the image data into model.predict in my CNN?

I have built and trained my CNN model and I want to test it.
I wrote a script that takes in an input image from the directory path specified, I then preprocessed the image and rescaled the pixel values to be between 0 and 1. I have also resized the image into the right dimensions and used the model.predict() to give a prediction. However when I run the code:
from keras.models import Sequential
from keras_preprocessing.image import *
from keras.layers import *
import tensorflow as tf
import numpy as np
from keras.layers.experimental.preprocessing import Rescaling
import os
import cv2
from keras.models import *
img_size = 250
#Load weights into new model
filepath = os.getcwd() + "/trained_model.h5"
model = load_model(filepath)
print("Loaded model from disk")
#Scales the pixel values to between 0 to 1
#datagen = ImageDataGenerator(rescale=1.0/255.0)
#Prepares Testing Data
testing_dataset = cv2.imread(os.getcwd() + "/cats and dogs images/single test sample/505.png")
#img = datagen.flow_from_directory(testing_dataset, target_size=(img_size,img_size))
img = cv2.resize(testing_dataset, (img_size,img_size))
newimg = np.asarray(img)
pixels = newimg.astype('float32')
pixels /= 255.0
print(pixels.shape)
model.predict(x=pixels)
this error pops up:
Loaded model from disk
(250, 250, 3)
Traceback (most recent call last):
File "C:\Users\Jackson\Documents\Programming\Python Projects\Neural Network That Deteremines Cats and Dogs\Test Trained Model.py", line 34, in <module>
model.predict(x=pixels)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1599, in predict
tmp_batch_outputs = predict_function(iterator)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__
result = self._call(*args, **kwds)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 696, in _initialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\function.py", line 3065, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1462 predict_function *
return step_function(self, iterator)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1452 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1445 run_step **
outputs = model.predict_step(data)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py:1418 predict_step
return self(x, training=False)
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:975 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs,
C:\Users\Jackson\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:191 assert_input_compatibility
raise ValueError('Input ' + str(input_index) + ' of layer ' +
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 250, 3]
What am I doing wrong or am I just missing something?
Also, I have tried the same with model.predict_classes() and model.predict_generator() but the same error appears.
If you're doing everything correct regarding the image input shape that it matches the required input shape of the model then it's most likely that the model expects to receive a batch of images of the size (250, 250, 3) so if you have an image that you want to test on the input shape should be of size (1, 250, 250, 3) this means you're passing a batch of size 1 image.
What your error message means is that the model expects an input shape of 4 dimensions and an input shape of 3 dimensions was passed, you need to include the batch dimension, so i think adding this line after the normalizing of the image should make it work.
pixels = np.expand_dims(pixels, axis=0)
At printing the shape line the pixels shape should be (1, 250, 250, 3)

tensorflow graph error when trying to generate data in transfer learning

I'm trying to use transfer learning on the pretrained inception model, so I created a class for feature extraction from the model:
from prototype import Dataset, VideoStreamHandler
import numpy
import random
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing import image
from scipy.misc import imresize
import time
class Extractor(Dataset.Dataset):
"""
"""
def __init__(self, path_to_data, seq_len, base_model, image_shape=(299, 299, 3)):
super().__init__(path_to_data, seq_len, input_shape=image_shape)
self._extractor = base_model
def extract_features(self, batch_size):
"""
passes the data through the base model to get the feature map to later train on
:return: feature map
"""
class_one_hot = self.one_hot_encode() # get the one hot for the classes
data = self.clean_data(self.get_data(), self._input_shape[0])
print("Processing {} videos".format(len(self.get_data())))
transfer_maps, labels = [], []
rand = random.SystemRandom()
while True:
for _ in range(batch_size):
row = rand.choice(data)
sequence = self.get_frames(row[0])
if len(sequence) > self._input_shape[0]:
sequence = self.rescale_frame_list(sequence, self._input_shape[0])
print("{} video processing is complete".format(row[0].split('\\')[-1]))
features = []
for frame in sequence:
frame_arr = image.img_to_array(frame) # turn image to numpy array
frame_arr = numpy.expand_dims(frame_arr, axis=0)
frame_arr = preprocess_input(frame_arr)
features.append(self._extractor.predict(frame_arr))
transfer_maps.append(features)
labels.append(class_one_hot[row[1]])
yield numpy.array(transfer_maps), numpy.array(labels)
def get_frames(self, pth):
"""
:type: string
:param pth: path to the specific file from which we take the frames
:return: the frames in the file
"""
f_queue = VideoStreamHandler.VideoStream(pth) # This object opens a thread that reads frames with opencv
# capture independently from the frame processing to prevent i/o delay and speed up processing
f_queue.start()
time.sleep(1.0) # wait a moment so the thread could start reading frames
sequence = []
while f_queue.isnt_empty():
frame = f_queue.read()
# resize is used to keep all frames from all videos the same size
frame = imresize(frame, (self._input_shape[1], self._input_shape[2]))
sequence.append(frame)
f_queue.close() # close the thread
return sequence
Then, I attempt to train a new model with keras's fit_generator:
my_model.fit_generator(generator=train_gen, epochs=10, steps_per_epoch=steps_per_epoch, verbose=1, workers=4)
However, I get this error:
Blockquote
Traceback (most recent call last):
File "C:/Users/Aviad Lazar/Desktop/project/prototype/transfer_learning.py", line 41, in
main()
File "C:/Users/Aviad Lazar/Desktop/project/prototype/transfer_learning.py", line 34, in main
my_model.fit_generator(generator=train_gen, epochs=10, steps_per_epoch=steps_per_epoch, verbose=1, workers=4)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\models.py", line 1315, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\engine\training.py", line 2194, in fit_generator
generator_output = next(output_generator)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\utils\data_utils.py", line 793, in get
six.reraise(value.class, value, value.traceback)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\utils\data_utils.py", line 658, in _data_generator_task
generator_output = next(self._generator)
File "C:\Users\Aviad Lazar\Desktop\project\prototype\FeatureExtractor.py", line 48, in extract_features
features.append(self._extractor.predict(frame_arr))
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\engine\training.py", line 1832, in predict
self._make_predict_function()
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\engine\training.py", line 1031, in _make_predict_function
**kwargs)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2506, in function
return Function(inputs, outputs, updates=updates, **kwargs)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\keras\backend\tensorflow_backend.py", line 2449, in init
with tf.control_dependencies(self.outputs):
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 4863, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 4481, in control_dependencies
c = self.as_graph_element(c)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3478, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\Aviad Lazar\Desktop\project\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3557, in _as_graph_element_locked
raise ValueError("Tensor %s is not an element of this graph." % obj)
ValueError: Tensor Tensor("global_average_pooling2d_1/Mean:0", shape=(?, 2048), dtype=float32) is not an element of this graph.

Resources