Я пытаюсь загрузить вес йоло, потому что хочу обнаружить транспортное средство, и это написано на анаконде. Код был написан следующим образом.
#all imports
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
%matplotlib inline
import keras
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.core import Flatten, Dense, Activation, Reshape
# Pre trained weights require this ordering
keras.backend.set_image_data_format('channels_first')
def model_keras():
model = Sequential()
# Layer 1
model.add(Convolution2D(16, 3, 3,input_shape=(3,448,448),border_mode='same',subsample=(1,1)))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Layer 2
model.add(Convolution2D(32,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))
# Layer 3
model.add(Convolution2D(64,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))
# Layer 4
model.add(Convolution2D(128,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))
# Layer 5
model.add(Convolution2D(256,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))
# Layer 6
model.add(Convolution2D(512,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),border_mode='valid'))
# Layer 7
model.add(Convolution2D(1024,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
# Layer 8
model.add(Convolution2D(1024,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
# Layer 9
model.add(Convolution2D(1024,3,3 ,border_mode='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(Flatten())
# Layer 10
model.add(Dense(256))
# Layer 11
model.add(Dense(4096))
model.add(LeakyReLU(alpha=0.1))
# Layer 12
model.add(Dense(1470))
return model
# Preprocessing
def preprocess(image):
cropped = image[300:650,500:,:] #cropping for selecting our area of interest as we don't detect cars in sky
resized = cv2.resize(cropped, (448,448)) #resizing to 448x448 image
normalized = 2.0*resized/255.0 - 1 #normalizing between -1 and 1
# The model works on (channel, height, width) ordering of dimensions
transposed = np.transpose(normalized, (2,0,1))
return transposed
def load_weights(model,yolo_weight_file):
data = np.fromfile(yolo_weight_file,np.float32)
data=data[4:]
index = 0
for layer in model.layers:
shape = [w.shape for w in layer.get_weights()]
if shape != []:
kshape,bshape = shape
bia = data[index:index+np.prod(bshape)].reshape(bshape)
index += np.prod(bshape)
ker = data[index:index+np.prod(kshape)].reshape(kshape)
index += np.prod(kshape)
layer.set_weights([ker,bia])
Пока все прошло хорошо, без ошибок.
# Load weights
model = model_keras()
load_weights(model,'D:/ndt/yolo-tiny.weights')
model.summary()
Однако эта ячейка, ошибка code "TypeError: объект типа 'NoneType' не имеет len ()".
TypeError Traceback (most recent call last)
<ipython-input-16-8e95a2ecf5ee> in <module>
1 # Load weights
2
----> 3 model = model_keras()
4 load_weights(model,'D:/ndt/yolo-tiny.weights')
5 model.summary()
<ipython-input-14-52b000880244> in model_keras()
3
4 # Layer 1
----> 5 model.add(Convolution2D(16, (3, 3), input_shape=(3,448,448),border_mode='same',subsample=(1,1)))
6 model.add(LeakyReLU(alpha=0.1))
7 model.add(MaxPooling2D(pool_size=(2, 2)))
D:\anaconda\lib\site-packages\keras\engine\sequential.py in add(self, layer)
164 # and create the node connecting the current layer
165 # to the input layer we just created.
--> 166 layer(x)
167 set_inputs = True
168 else:
D:\anaconda\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
487 # Actually call the layer,
488 # collecting output(s), mask(s), and shape(s).
--> 489 output = self.call(inputs, **kwargs)
490 output_mask = self.compute_mask(inputs, previous_mask)
491
D:\anaconda\lib\site-packages\keras\layers\convolutional.py in call(self, inputs)
169 padding=self.padding,
170 data_format=self.data_format,
--> 171 dilation_rate=self.dilation_rate)
172 if self.rank == 3:
173 outputs = K.conv3d(
D:\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py in conv2d(x, kernel, strides, padding, data_format, dilation_rate)
3700 data_format = normalize_data_format(data_format)
3701
-> 3702 x, tf_data_format = _preprocess_conv2d_input(x, data_format)
3703
3704 padding = _preprocess_padding(padding)
D:\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py in _preprocess_conv2d_input(x, data_format, force_transpose)
3573 tf_data_format = 'NHWC'
3574 if data_format == 'channels_first':
-> 3575 if not _has_nchw_support() or force_transpose:
3576 x = tf.transpose(x, (0, 2, 3, 1)) # NCHW -> NHWC
3577 else:
D:\anaconda\lib\site-packages\keras\backend\tensorflow_backend.py in _has_nchw_support()
521 """
522 explicitly_on_cpu = _is_current_explicit_device('cpu')
--> 523 gpus_available = len(_get_available_gpus()) > 0
524 return (not explicitly_on_cpu and gpus_available)
525
TypeError: object of type 'NoneType' has no len()
Поиск в нескольких местах не помог. Как исправить?