Not able to reshape the image in mnist dataset using sklean
This is the starting portion of my code just load the data
some_digit = X[880]
some_digit_image = some_digit.reshape(28, 28)
ERROR PART
ValueError Traceback (most recent call last)
<ipython-input-15-4d618bdb57bc> in <module>
1 some_digit = X[880]
----> 2 some_digit_image = some_digit.reshape(28,28)
ValueError: cannot reshape array of size 64 into shape (28,28)
You can only reshape it into a 8, 8 array. 8x8=64
try:
some_digit = X[880]
some_digit_image = some_digit.reshape(8, 8)
Related
I have extracted the weights of a specific model from a .pb file. It gives me all the weights trucked in a variable in bytes format. as below:
weights = b'\n\x1b\n\t\x08\x01\x12\x05model\n\x0e\x08\x02\x12\nsignatures\n\xe2\x01\n\x18\x08\x03\x12\x14layer_with_weights-0\n\x0b\x08\x03\x12\x07layer-0\n\x0b\x08\x04\x12\x07layer-1\n\x18\x08\x05\x12\x14layer_with_weights-1\n\x0b\x08\x05\x12\x07layer-2\n\r\x08\x06\x12\tvariables\n\x17\x08\x07\x12\x13trainable_variables\n\x19\x08\x08\x12\x15regularization_losses\n\r\x08\t\x12\tkeras_api\n\x0e\x08\n\x12\nsignatures\n#\x08\x0b\x12\x1f_self_saveable_object_factories\n\x00\n\x92R\n\x0b\x08\x0c\x12\x07layer-0\n\x0b\x08\r\x12\x07layer-1\n\x18\x08\x0e\x12\x14layer_with_weights-0\n\x0b\x08\x0e\x12\x07layer-2\n\x0b\x08\x0f\x12\x07layer-3\n\x18\x08\x10\x12\x14layer_with_weights-1\n\x0b\x08\x10\x12\x07layer-4\n\x18\x08\x11\x12\x14layer_with_weights-2\n\x0b\x08\x11\x12\x07layer-5\n\x0b\x08\x12\x12\x07layer-6\n\x18\x08\x13\x12\x14layer_with_weights-3\n\x0b\x08\x13\x12\x07layer-7\n\x18\x08\x14\x12\x14layer_with_weights-4\n\x0b\x08\x14\x12\x07layer-8\n\x0b\x08\x15\x12\x07layer-9\n\x0c\x08\x16\x12\x08layer-10\n\x0c\x08\x17\x12\x08layer-11\n\x18\x08\x18\x12\x14layer_with_weights-5\n\x0c\x08\x18\x12\x08layer-12\n\x18\x08\x19\x12\x14layer_with_weights-6\n\x0c\x08\x19\x12\x08layer-13\n\x0c\x08\x1a\x12\x08layer-14\n\x18\x08\x1b\x12\x14layer_with_weights-7\n\x0c\x08\x1b\x12\x08layer-15\n\x18\x08\x1c\x12\x14layer_with_weights-8\n\x0c\x08\x1c\x12\x08layer-16\n\x18\x08\x1d\x12\x14layer_with_weights-9\n\x0c\x08\x1d\x12\x08layer-17\n\x19\x08\x1e\x12\x15layer_with_weights-10\n\x0c\x08\x1e\x12\x08layer-18\n\x0c\x08\x1f\x12\x08layer-19\n\x0c\x08 \x12\x08layer-20\n\x0c\x08!\x12\x08layer-21\n\x19\x08"\x12\x15layer_with_weights-11\n\x0c\x08"\x12\x08layer-22\n\x19\x08#\x12\x15layer_with_weights-12\n\x0c\x08#\x12\x08layer-23\n\x0c\x08$\x12\x08layer-24\n\x19\x08...
I have tried to convert it using array like this:
import array
arr = array.array('f', weights)
However, I get the following error:
Traceback (most recent call last):
File "/tmp/ipykernel_4441/2375324399.py", line 1, in <module>
arr = array.array('f', value)
ValueError: bytes length not a multiple of item size
I am trying to Read an Image using GeneralizedRCNN, Input shape is given as a comment with code. The problem is I am getting an error while tracing the model with input shape. The error is :
> trace = torch.jit.trace(model, input_batch) line Providing the error
> "/usr/local/lib/python3.7/dist-packages/torch/tensor.py:467:
> RuntimeWarning: Iterating over a tensor might cause the trace to be
> incorrect. Passing a tensor of different shape won't change the number
> of iterations executed (and might lead to errors or silently give
> incorrect results). 'incorrect results).', category=RuntimeWarning)
> --------------------------------------------------------------------------- IndexError Traceback (most recent call
> last) <ipython-input-25-52ff7ef794de> in <module>()
> 1 #First attempt at tracing
> ----> 2 trace = torch.jit.trace(model, input_batch)
>
> 7 frames
> /usr/local/lib/python3.7/dist-packages/detectron2/modeling/meta_arch/rcnn.py
> in <listcomp>(.0)
> 182 Normalize, pad and batch the input images.
> 183 """
> --> 184 images = [x["image"].to(self.device) for x in batched_inputs]
> 185 images = [(x - self.pixel_mean) / self.pixel_std for x in images]
> 186 images = ImageList.from_tensors(images, self.backbone.size_divisibility)
>
> IndexError: too many indices for tensor of dimension 3
model = build_model(cfg)
model.eval()
# print(model)
input_image = Image.open("model/xxx.jpg")
display(input_image)
to_tensor = transforms.ToTensor()
input_tensor = to_tensor(input_image)
# input_tensor.size = torch.Size([3, 519, 1038])
input_batch = input_tensor.unsqueeze(0)
# input_batch.size = torch.Size([1, 3, 519, 1038])
trace = torch.jit.trace(model, input_batch)
This error occurred because input_batch.size = torch.Size([1, 3, 519, 1038]) has 4 dimensions and trace = torch.jit.trace(model, input_batch) expected to get a 3 dimensions as input.
you don't need input_batch = input_tensor.unsqueeze(0). delete or comment this line.
By default
..
The torch.jit.trace function cannot be used directly. However, it does provide a wrapper called that the model can take a tensor or a tuple of tensors as input. You can find a way to use it because of them.
The code for tracing the Mask RCNN model looks like this:
import torch
import torchvision
from detectron2.export.flatten import TracingAdapter
def inference_func(model, image):
inputs= [{"image": image}]
return model.inference(inputs, do_postprocess=False)[0]
print("cfg.MODEL.WEIGHTS: ",cfg.MODEL.WEIGHTS) ## RETURNS : cfg.MODEL.WEIGHTS: drive/Detectron2/model_final.pth
model= build_model(cfg)
example= torch.rand(1, 3, 224, 224)
wrapper= TracingAdapter(model, example, inference_func)
wrapper.eval()
traced_script_module= torch.jit.trace(wrapper, (example,))
traced_script_module.save("drive/Detectron2/model-final.pt")
I had been trying to replicated an online tutorial for plotting confusion matrix but got recursion error, tried resetting the recursion limit but still the error persists. The code is a below:
log = LogisticRegression()
log.fit(x_train,y_train)
pred_log = log.predict(x_train)
confusion_matrix(y_train,pred_log)
The error I got is :
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
<ipython-input-57-4b8fbe47e72d> in <module>
----> 1 (confusion_matrix(y_train,pred_log))
<ipython-input-48-92d5242f8580> in confusion_matrix(test_data, pred_data)
1 def confusion_matrix(test_data,pred_data):
----> 2 c_mat = confusion_matrix(test_data,pred_data)
3 return pd.DataFrame(c_mat)
... last 1 frames repeated, from the frame below ...
<ipython-input-48-92d5242f8580> in confusion_matrix(test_data, pred_data)
1 def confusion_matrix(test_data,pred_data):
----> 2 c_mat = confusion_matrix(test_data,pred_data)
3 return pd.DataFrame(c_mat)
RecursionError: maximum recursion depth exceeded
The shape of the train and test data is as below
x_train.shape,y_train.shape,x_test.shape,y_test.shape
# ((712, 7), (712,), (179, 7), (179,))
Tried with: sys.setrecursionlimit(1500)
But still no resolution.
Looks like you are recursively calling the same function. Try changing the outer function name.
1 def confusion_matrix(test_data,pred_data):
----> 2 c_mat = confusion_matrix(test_data,pred_data)
3 return pd.DataFrame(c_mat)
To
def confusion_matrix_pd_convertor(test_data,pred_data):
c_mat = confusion_matrix(test_data,pred_data)
return pd.DataFrame(c_mat)
log = LogisticRegression()
log.fit(x_train,y_train)
pred_log = log.predict(x_train)
confusion_matrix_pd_convertor(y_train,pred_log)
ValueError Traceback (most recent call last) in () ----> 1 prediction = model.predict(image_resized.reshape(1,50,50,3)) 2 print('Prediction Score:\n',prediction[0]) ValueError: cannot reshape array of size 2352 into shape (1,50,50,3)
By just looking at what you have posted you should replace image_resized.reshape(1,50,50,3) with image_resized.reshape(1,28,28,3)
I am classifying images into 30 different classes using CNN and on running the CNN ,I am getting the following traceback-
Traceback (most recent call last):
File "thapar.py", line 165, in <module>
Y = np_utils.to_categorical(labels, num_classes)
File "/home/rishav/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/utils/np_utils.py", line 31, in to_categorical
categorical[np.arange(n), y] = 1
IndexError: index 10 is out of bounds for axis 1 with size 10
Python 3.6
Keras 2.1.5 using tensorflow backend
Ubuntu 16.04
I have explored other similar questions but unable to find the similarity in issue.
How to resolve this issue?
Edit:
num_classes = 30
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')
labels[0:20]=0
labels[20:40]=1
labels[40:60]=2
labels[60:80]=3
labels[80:100]=4
labels[100:120]=5
labels[120:140]=6
labels[140:160]=7
labels[160:180]=8
labels[180:200]=9
labels[200:220]=10
labels[220:240]=11
labels[240:260]=12
labels[260:280]=13
labels[280:300]=14
labels[300:320]=15
labels[320:340]=16
labels[340:360]=17
labels[360:380]=18
labels[380:400]=19
labels[400:420]=20
labels[420:440]=21
labels[440:460]=22
labels[460:480]=23
labels[480:500]=24
labels[500:520]=25
labels[520:540]=26
labels[540:560]=27
labels[560:580]=28
labels[580:600]=29
names =
['bent','bowl','claw','fist','good','thumbs_up','A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']
Y = np_utils.to_categorical(labels, num_classes)
Recently, I have faced the same problem, then I got to know it was giving error with some images only, I remove those images and it worked perfectly for me then.