ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)
  • 15-May-2023
Lightrun Team
Author Lightrun Team
Share
ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)

ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)

Lightrun Team
Lightrun Team
15-May-2023

Explanation of the problem

 

When attempting to convert custom YOLOv4 weights to TensorFlow using the command “python save_model.py –weights ./data/yolo-obj_best.weights –output ./checkpoints/yolov4-704 –input_size 704 –model yolov4,” the user encountered an error in their code. Specifically, the error occurred in the “save_model.py” file on line 58, with the message “ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1).” The error message indicates that the program was unable to reshape an array of size 2048 into the specified shape (18, 1024, 1, 1).

 

Troubleshooting with the Lightrun Developer Observability Platform

 

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: Warning: ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)

 

To resolve this issue, one potential solution would be to adjust the dimensions of the array to match the specified shape. Additionally, it may be helpful to review the data and ensure that it is properly formatted and aligned with the expected input and output sizes of the program. Below is an example code block that could be used to adjust the dimensions of the array:

 

import numpy as np

# Assuming conv_weights is the array causing the error
new_shape = (18, 1024, 1, 1)
current_shape = conv_weights.shape

# If the current shape doesn't match the desired shape, adjust it accordingly
if current_shape != new_shape:
  # Reshape the array to match the new shape
  conv_weights = np.reshape(conv_weights, new_shape)
  # Transpose the array to match the expected dimensions
  conv_weights = np.transpose(conv_weights, (2, 3, 1, 0))

 

Other popular problems with tensorflow-yolov4-tflite

Problem 1: “ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1)” One of the most common issues when converting YOLOv4 weights to TensorFlow using the tensorflow-yolov4-tflite repository is encountering the error “ValueError: cannot reshape array of size 2048 into shape (18,1024,1,1).” This error occurs when the shape of the conv_weights array cannot be properly reshaped to match the specified conv_shape. This problem can be solved by modifying the code in the load_weights function in the utils.py file to ensure that the conv_shape is properly calculated based on the shape of the conv_weights array.
def load_weights(model, weights_file, model_name, tiny=False):
    #...
    for i in range(conv_num):
        conv_layer = model.get_layer('conv2d_%d' % (i + 1))
        conv_shape = conv_layer.get_weights()[0].shape

        if i not in output_layers:
            #...
            conv_weights = load_conv_weights(conv_layer, weights_file, start)

            if len(conv_shape) == 4:
                conv_weights = conv_weights.reshape(conv_shape).transpose([3, 2, 0, 1])
            elif len(conv_shape) == 3:
                conv_weights = conv_weights.reshape(conv_shape).transpose([2, 1, 0])
            conv_layer.set_weights([conv_weights, conv_layer.get_weights()[1]])

            start = end
    #...
Problem 2: “AttributeError: ‘Tensor’ object has no attribute ‘read'” Another common issue that users face when working with tensorflow-yolov4-tflite is the error “AttributeError: ‘Tensor’ object has no attribute ‘read’.” This error occurs when attempting to read a file using the tf.io.gfile.GFile() function and passing a tensor object instead of a string as the first argument. The solution to this problem is to use the tf.io.read_file() function to read the file instead of tf.io.gfile.GFile().
import tensorflow as tf

def read_image_file(filename):
    file_contents = tf.io.read_file(filename)
    image = tf.image.decode_image(file_contents, channels=3)
    return image
Problem 3: “TypeError: can’t pickle weakref objects” Another issue that users may face when using tensorflow-yolov4-tflite is the error “TypeError: can’t pickle weakref objects.” This error occurs when attempting to save or load a model using the pickle module and the model contains weakref objects. The solution to this problem is to use the joblib module instead of pickle to save and load the model.
import joblib

# Save the model
joblib.dump(model, 'model.joblib')

# Load the model
model = joblib.load('model.joblib')

A brief introduction to tensorflow-yolov4-tflite

 

TensorFlow-YOLOv4-TFLite is an open-source project that provides a TensorFlow implementation of the popular You Only Look Once version 4 (YOLOv4) object detection algorithm. It is designed to enable developers to easily train and deploy custom object detection models using TensorFlow and convert them to the TFLite format for use on mobile and edge devices. The project is built on top of the TensorFlow 2.0 framework and includes pre-trained weights for the COCO dataset.

The TensorFlow-YOLOv4-TFLite project provides a number of features that make it an attractive choice for object detection tasks. For example, it supports training with multiple GPUs, automatic mixed precision training to improve performance, and the ability to generate visualizations of the detection results. Additionally, it provides several options for post-processing the detection results, such as non-maximum suppression (NMS) and filtering by confidence scores. Overall, the project offers a flexible and powerful solution for developers looking to implement custom object detection models in TensorFlow.

 

Most popular use cases for tensorflow-yolov4-tflite

 

  1. Object detection: tensorflow-yolov4-tflite can be used to perform object detection on images and videos. It uses the YOLOv4 algorithm, which is known for its accuracy and speed, to detect objects in real-time. Here’s an example code block that demonstrates how to perform object detection using tensorflow-yolov4-tflite:

 

import tensorflow as tf
from yolov4.tf import YOLOv4

yolo = YOLOv4()
yolo.classes = "data/classes/obj.names"
yolo.make_model()
yolo.load_weights("checkpoints/yolov4-416")

image = tf.image.decode_image(open("test.jpg", "rb").read(), channels=3)
image = tf.expand_dims(image, 0)

boxes, scores, classes, nums = yolo.predict(image)

print(f"Found {nums[0]} objects")
for i in range(nums[0]):
    print(f"Object {i}: class={classes[0][i]}, score={scores[0][i]}, box={boxes[0][i]}")

 

  1. Model conversion: tensorflow-yolov4-tflite can be used to convert YOLOv4 models from their original format (such as Darknet or TensorFlow) to other formats such as TensorFlow Lite. This can be useful for deploying models on mobile and embedded devices that have limited computational resources. Here’s an example code block that demonstrates how to convert a YOLOv4 TensorFlow model to TensorFlow Lite:

 

import tensorflow as tf
from tensorflow import keras

converter = tf.lite.TFLiteConverter.from_saved_model("yolov4/saved_model")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, 
  tf.lite.OpsSet.SELECT_TF_OPS
]
tflite_model = converter.convert()

open("yolov4.tflite", "wb").write(tflite_model)

 

  1. Fine-tuning: tensorflow-yolov4-tflite can be used to fine-tune pre-trained YOLOv4 models on custom datasets. This can be useful when the objects to be detected are different from the ones that the original model was trained on. Here’s an example code block that demonstrates how to fine-tune a pre-trained YOLOv4 model on a custom dataset:

 

import tensorflow as tf
from yolov4.tf import YOLOv4

yolo = YOLOv4(tiny=False)
yolo.classes = "data/classes/obj.names"
yolo.make_model()
yolo.load_weights("checkpoints/yolov4.h5")

train_dataset = tf.data.Dataset.from_tensor_slices(...)
val_dataset = tf.data.Dataset.from_tensor_slices(...)
yolo.compile(loss="binary_crossentropy", optimizer="adam")
yolo.fit(train_dataset, validation_data=val_dataset, epochs=10)

yolo.save_weights("checkpoints/yolov4_finetuned.h5")
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.