I'm facing trouble when training a model using pre-trained inceptionV3 for my own image data set.
I'm loading images using data.Dataset loader and 'transforms' for image transformation.
Here's my inceptionV3 model
inceptionV3 = torchvision.models.inception_v3(pretrained=True)
pretrained_model = nn.Sequential(*list(inceptionV3.children()))
pretrained_model = nn.Sequential(*list(pretrained_features.children())[:-1])
for param in pretrained_model.parameters(): param.requires_grad = False
Here's my transforms code
data_transforms = transforms.Compose([
transforms.Scale((299,299)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
I'm getting this error at the line 'out_features = pretrained_model(inputs)'
Traceback (most recent call last): File "build_models.py", line 42, in <module> use_gpu=use_gpu)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 488, in create_attributes_model batch_size, num_workers, num_epochs, use_gpu=use_gpu)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 276, in train_model flatten_pretrained_out=flatten_pretrained_out)
File "/home/ubuntu/apparel-styles/ml_src/classifiers.py", line 181, in train_attribute_model out_features = pretrained_model(inputs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 68, in forward outputs = self.parallel_apply(replicas, inputs, kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py", line 78, in parallel_apply return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 67, in parallel_apply raise output
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", line 42, in _worker output = module(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/container.py", line 67, in forward input = module(input)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torchvision/models/inception.py", line 312, in forward x = self.fc(x)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 325, in __call__ result = self.forward(*input, **kwargs)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward return F.linear(input, self.weight, self.bias)
File "/home/ubuntu/apparel-styles/env/venv/lib/python3.6/site-packages/torch/nn/functional.py", line 835, in linear return torch.addmm(bias, input, weight.t()) RuntimeError: size mismatch at /pytorch/torch/lib/THC/generic/THCTensorMathBlas.cu:243
After getting this error, I tried to print the input size. It's value is
print(inputs.shape)
torch.Size([256, 3, 299, 299])
I have a list which looks like this,
data_raw=[[], [7944, -11896, 3376, 1627, -850, -3991], [8688, -12192, 1936,1404, -616, -3536], [6540, -11800, 1608, 3021, 780, -1061], [6804, -11864, 3828, 4310, 552, -2343], [7208, -12544, 3768, 2542, 286, 1264], [7048, -14532, 6824, 2528, 1577, 2583], [6112, -17376, 10180, 132, -1716, 1001], [7576, -21140, 6796, -1725, 1657, 1980], [2928, -31716, 15400, -5945, 824, -3558], [8940, -24016, 11540, -12047,-5574, -16019], [12020, -17516, 3744, -14637, 1521, -14791], [8916, -16160, 5860, -14122, -3793, -13597], [10144, -8124, 1076, -12027, -1194, -8809], [8088, -7264, 928, -18441, -2058, -80], [7684, -4896, -5224, -9800, 2427, 2054], [2040, -7776, -3520, -9306, 4442, 1276], [6240, -7340, -7216, -1757, -3630, -2734], [5720, -3940, -4632, -901, 1469, -1682], [5244, -4676, -5648, 2720, 3526, -436], [4016, -5336, -2976, 4280, 4543, -1562], [4028, -5156, -5560, 7391, 5000, -1317], [748, -9800, -2144, 10353, 6616, -3390], [10268, -7220, 1844, 11657, 8566, -4740], [11300, -10752, 4508, 11666, 10771, -1356], [16792, -10180, 24476, 13474, 2828, -5205], [19208, -10908, 6636, 9747, 10501, 1676], [7540, -20480, 13248, 8715, 12607, 7017], [15780, -20832, 11600, 5686, 4737, -3654], [18004, -20072, 17716, 1082, 2218, -3181], [16516, -18528, 14568, -3931, -5457, -4260], [15596, -12596,9084, -7735, -8646, -4221], [13296, -8948, 6316, -9215, -8260, -3225], [10860, -8124, 6116, -7264, -7653, -678], [7968, -7828, 5384, -8604, -7043, 1076], [8008, -5316, 1816, -6457, -7414, -50], [9304, -3568, 1092, -4895, -4654, 3123], [9560, -3932, -352, -904, -6369, 1981], [14692, -3168, 836, 2406, -8099, 3121], [13088, -6292, 44, 5503, -11759, 6405], [11892, -8316, -836, 6159, -8673, 10130], [8252, -13220, -1064, 8279, -7906, 12090], [3572, -18392, -1536, 5995, -2719, 10667], [2864, -19576, 960, 6207, -4501, 6554], [1024, -20140, -1964, 7834, -10817, 5197]]
When i use this code:
data = np.array(data_raw).astype(float)
I got an error:
>> Traceback (most recent call last):
data = np.array(data_raw).astype(float)
ValueError: setting an array element with a sequence.
Does anyone know why this error occurred?
The first element of your 2D list is an empty list, causing this issues. If you remove it or just use
data = np.array(data_raw[1:]).astype(float)
it will work as intended.
Is it possible in keras to use tensorflow tensor manipulations commands for altering the shape of data, of split by an axis or somethign like that..
I am currently trying something like this:
def row_convolution(input):
filter_size = 8
temp_list = []
input = tf.placeholder(tf.float32,shape=(None,33,1,8,45,3))
for section in tf.unstack(input,axis=1):
section = tf.reshape(section,[1,8,45,3])
temp_list.append((Conv2D(filters = 1, kernel_size = (8,45), activation='relu' , name = 'conv')(section)))
print "len: " + str(len(temp_list))
return tf.convert_to_tensor(np.array(temp_list))
Which is is the function for a lamdba layer, but the last command give me an error message stating:
Traceback (most recent call last):
File "keras_cnn_phoneme_original_fit_generator.py", line 182, in <module>
fws()
File "keras_cnn_phoneme_original_fit_generator.py", line 167, in fws
model.add(Lambda(row_convolution,input_shape = (33,1,8,45,3)))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 422, in add
layer(x)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 554, in __call__
output = self.call(inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 659, in call
return self.function(inputs, **arguments)
File "keras_cnn_phoneme_original_fit_generator.py", line 147, in row_convolution
return tf.convert_to_tensor(np.array(temp_list))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 637, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 702, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 110, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 99, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 451, in make_tensor_proto
append_fn(tensor_proto, proto_values)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 109, in SlowAppendObjectArrayToTensorProto
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <tf.Tensor 'lambda_1/conv/Relu:0' shape=(1, 1, 1, 1) dtype=float32>
This is my class Stat. I am adding a new compute property to the class which will automatically sum up various counts in the existing class
class Stat(ndb.Model):
visit_count = ndb.IntegerProperty(default=0)
exit_count = ndb.IntegerProperty(default=0)
# New code
def _get_total_count(self):
return self.visit_count + self.exit_count
response_count = ndb.ComputedProperty(lambda self: self._get_total_count)
When I create an instance of this class and save it
stat = Stat(visit_count=0, exit_count=2)
stat.put()
I get this exception:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
return repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
rep = prop._value_to_repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
return repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
rep = prop._value_to_repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
return repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
rep = prop._value_to_repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
return repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 3013, in __repr__
rep = prop._value_to_repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 1207, in _value_to_repr
return repr(val)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/key.py", line 357, in __repr__
if self.app() != _DefaultAppId():
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/key.py", line 824, in _DefaultAppId
return os.getenv('APPLICATION_ID', '_')
File "/Users/antkong/dev/zeetings/zeetings-ve/bin/../lib/python2.7/os.py", line 515, in getenv
return environ.get(key, default)
File "/Users/antkong/dev/zeetings/zeetings-ve/bin/../lib/python2.7/UserDict.py", line 58, in get
def get(self, key, failobj=None):
RuntimeError: maximum recursion depth exceeded
If I remove the compute property, put can be executed without issue.
How can I fix this issue?
response_count = ndb.ComputedProperty(lambda self: self._get_total_count)
Means that the value you're returning for your ComputedProperty is the function self._get_total_count - you're not calling it.
response_count = ndb.ComputedProperty(lambda self: self._get_total_count())
The code:
class StockFactory(UniqueObjectsFactory):
FACTORY_FOR = Stock
FACTORY_DJANGO_GET_OR_CREATE = ('name', 'market')
market = factory.SubFactory(MarketFactory)
symbol = FuzzyAttribute(lambda: ''.join(random.choice(string.ascii_uppercase) for _ in xrange(4)))
name = FuzzyCompanyName()
# last_trade_price = fuzzy.FuzzyDecimal(0.0, 10000.0)
class PositionsFactory(FreezeableFactory):
FACTORY_FOR = Position
FACTORY_DJANGO_GET_OR_CREATE = ('stock','AnotherObject')
id = FuzzyInteger(100000)
stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject) #If I comment stock out it would fail here
created_date = FuzzyDateTime(start_dt=datetime(2013, 1, 1, tzinfo=compat.UTC))
The error:
File "/home/alon/Projects/stox-server/execution/tests/functional/test_positions.py", line 21, in setUp
PositionsFactory.create( portfolio=self.portfolio)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 522, in create
attrs = cls.attributes(create=True, extra=kwargs)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
force_sequence=force_sequence,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 283, in build
return stub.__fill__()
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 83, in __fill__
res[attr] = getattr(self, attr)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
val = val.evaluate(self, self.__containers)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
containers=containers,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 75, in evaluate
return self.function(obj)
File "/home/alon/Projects/stox-server/execution/tests/common/factories.py", line 173, in <lambda>
symbol = factory.LazyAttribute(lambda pos: pos.stock.symbol)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
val = val.evaluate(self, self.__containers)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
containers=containers,
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 299, in evaluate
return self.generate(sequence, obj, create, defaults)
File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 386, in generate
return subfactory.simple_generate(create, **params)
AttributeError: type object 'Stock' has no attribute 'simple_generate'
Any clues? Ideas? I work with factory-boy quite a lot and most of the time it's an excellent tool. but after hours of debugging I just cant find the problem
So stupid of me:
Those line are wrong:
stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject)
Should have been:
stock = factory.SubFactory(StockFactory)
AnotherObject = factory.SubFactory(AnotherObjectFactory)
Hope it helps anyone else who bumps into this issue