Я создал более быструю модель r-cnn и сейчас маркирую изображения множеством ограничивающих рамок. Моя проблема заключается в том, что ядро Python в конечном итоге дает сбой при маркировке первых ~ 3000 из 70 000 изображений. Я сузил проблему до image = cv2.imread(). Независимо от использования del image или image = None, чрезмерное использование памяти сохраняется. Есть ли проблема с утечкой памяти в библиотеке CV2? Я использую версию 3.3. Можно ли как-нибудь очистить память? Спасибо.
def SaveFigureAsImage(fileName,fig=None,**kwargs):
''' Save a Matplotlib figure as an image without borders or frames.
Args:
fileName (str): String that ends in .png etc.
fig (Matplotlib figure instance): figure you want to save as the image
Keyword Args:
orig_size (tuple): width, height of the original image used to maintain
aspect ratio.
'''
a=fig.gca()
a.set_frame_on(False)
a.set_xticks([]); a.set_yticks([])
plt.axis('off')
fig.savefig(fileName, transparent=True, bbox_inches='tight', \
pad_inches=0)
#classes
classes = {
redacted
}
#load model
image_input = keras.layers.Input((None, None, 3))
model = keras_retinanet.models.ResNet50RetinaNet(image_input, num_classes=len(classes), weights='imagenet')
model.load_weights('snapshots/resnet50_model_1_6.h5')
test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()
#possibely resize the image
test_generator = PascalVocIterator(
'../',
'test',
test_image_data_generator,
classes=classes
)
#stats
frequency = {}
ef read_image_bgr(path):
image = np.asarray(PIL.Image.open(path).convert('RGB'))
return image[:, :, ::-1].copy()
def preprocess_image(x):
# mostly identical to "https://github.com/fchollet/keras/blob/ ... t_utils.py"
# except for converting RGB -> BGR since we assume BGR already
x = x.astype(keras.backend.floatx())
if keras.backend.image_data_format() == 'channels_first':
if x.ndim == 3:
x[0, :, :] -= 103.939
x[1, :, :] -= 116.779
x[2, :, :] -= 123.68
else:
x[:, 0, :, :] -= 103.939
x[:, 1, :, :] -= 116.779
x[:, 2, :, :] -= 123.68
else:
x[..., 0] -= 103.939
x[..., 1] -= 116.779
x[..., 2] -= 123.68
return x
def random_transform(
image,
boxes,
image_data_generator,
seed=None
):
if seed is None:
seed = np.random.randint(10000)
image = image_data_generator.random_transform(image, seed=seed)
# set fill mode so that masks are not enlarged
fill_mode = image_data_generator.fill_mode
image_data_generator.fill_mode = 'constant'
for index in range(boxes.shape[0]):
# generate box mask and randomly transform it
mask = np.zeros_like(image, dtype=np.uint8)
b = boxes[index, :4].astype(int)
assert(b[0] < b[2] and b[1] < b[3]), 'Annotations contain invalid box: {}'.format(b)
assert(b[2] = beg and count - beg < 2000:
#image = cv2.imread(p) #this is the memory leak
image = np.array(Image.open(p))
image_copy = image.copy()
#preprocess image
image_copy = preprocess_image(image_copy)
image, scale = resize_image(image, 600, 600)
image_copy, scale = resize_image(image_copy, 600, 600)
name = p.split('/')[7]
name = name.split('.jpg')[0]
start = time.time()
_, _, detections = model.predict_on_batch(np.expand_dims(image_copy, axis=0))
print("processing time: ", time.time() - start)
im = image.copy()
predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
scores = np.max(detections[0, :, 4:], axis=1)
for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
#print(label, score)
if score < 0.25: #change the 0.5 cutoff to a lower number
continue
b = detections[0, idx, :4].astype(int)
caption = "{} {:.3f}".format(test_generator.labels[label], score)
if label == 0:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (255, 0, 0), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 2:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 3:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 120, 120), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 4:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 120, 120), 3)
if "pocks" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
else:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(im)
print(name)
SaveFigureAsImage('../processed_data/' + name, plt.gcf() )
plt.close("all")
print("----------------------------")
#print(gc.collect())
Подробнее здесь: https://stackoverflow.com/questions/477 ... cv2-imread
Проблемы использования памяти с CV2.imread() ⇐ Python
Программы на Python
-
Anonymous
1734301808
Anonymous
Я создал более быструю модель r-cnn и сейчас маркирую изображения множеством ограничивающих рамок. Моя проблема заключается в том, что ядро Python в конечном итоге дает сбой при маркировке первых ~ 3000 из 70 000 изображений. Я сузил проблему до image = cv2.imread(). Независимо от использования del image или image = None, чрезмерное использование памяти сохраняется. Есть ли проблема с утечкой памяти в библиотеке CV2? Я использую версию 3.3. Можно ли как-нибудь очистить память? Спасибо.
def SaveFigureAsImage(fileName,fig=None,**kwargs):
''' Save a Matplotlib figure as an image without borders or frames.
Args:
fileName (str): String that ends in .png etc.
fig (Matplotlib figure instance): figure you want to save as the image
Keyword Args:
orig_size (tuple): width, height of the original image used to maintain
aspect ratio.
'''
a=fig.gca()
a.set_frame_on(False)
a.set_xticks([]); a.set_yticks([])
plt.axis('off')
fig.savefig(fileName, transparent=True, bbox_inches='tight', \
pad_inches=0)
#classes
classes = {
redacted
}
#load model
image_input = keras.layers.Input((None, None, 3))
model = keras_retinanet.models.ResNet50RetinaNet(image_input, num_classes=len(classes), weights='imagenet')
model.load_weights('snapshots/resnet50_model_1_6.h5')
test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()
#possibely resize the image
test_generator = PascalVocIterator(
'../',
'test',
test_image_data_generator,
classes=classes
)
#stats
frequency = {}
ef read_image_bgr(path):
image = np.asarray(PIL.Image.open(path).convert('RGB'))
return image[:, :, ::-1].copy()
def preprocess_image(x):
# mostly identical to "https://github.com/fchollet/keras/blob/master/keras/applications/imagenet_utils.py"
# except for converting RGB -> BGR since we assume BGR already
x = x.astype(keras.backend.floatx())
if keras.backend.image_data_format() == 'channels_first':
if x.ndim == 3:
x[0, :, :] -= 103.939
x[1, :, :] -= 116.779
x[2, :, :] -= 123.68
else:
x[:, 0, :, :] -= 103.939
x[:, 1, :, :] -= 116.779
x[:, 2, :, :] -= 123.68
else:
x[..., 0] -= 103.939
x[..., 1] -= 116.779
x[..., 2] -= 123.68
return x
def random_transform(
image,
boxes,
image_data_generator,
seed=None
):
if seed is None:
seed = np.random.randint(10000)
image = image_data_generator.random_transform(image, seed=seed)
# set fill mode so that masks are not enlarged
fill_mode = image_data_generator.fill_mode
image_data_generator.fill_mode = 'constant'
for index in range(boxes.shape[0]):
# generate box mask and randomly transform it
mask = np.zeros_like(image, dtype=np.uint8)
b = boxes[index, :4].astype(int)
assert(b[0] < b[2] and b[1] < b[3]), 'Annotations contain invalid box: {}'.format(b)
assert(b[2] = beg and count - beg < 2000:
#image = cv2.imread(p) #this is the memory leak
image = np.array(Image.open(p))
image_copy = image.copy()
#preprocess image
image_copy = preprocess_image(image_copy)
image, scale = resize_image(image, 600, 600)
image_copy, scale = resize_image(image_copy, 600, 600)
name = p.split('/')[7]
name = name.split('.jpg')[0]
start = time.time()
_, _, detections = model.predict_on_batch(np.expand_dims(image_copy, axis=0))
print("processing time: ", time.time() - start)
im = image.copy()
predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
scores = np.max(detections[0, :, 4:], axis=1)
for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
#print(label, score)
if score < 0.25: #change the 0.5 cutoff to a lower number
continue
b = detections[0, idx, :4].astype(int)
caption = "{} {:.3f}".format(test_generator.labels[label], score)
if label == 0:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (255, 0, 0), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 2:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 3:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 120, 120), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
elif label == 4:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 120, 120), 3)
if "pocks" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
else:
cv2.rectangle(im, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 3)
if "redacted" not in frequency:
frequency["redacted"] = 1
else:
frequency["redacted"] += 1
cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
cv2.putText(im, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
plt.figure(figsize=(10, 10))
plt.axis('off')
plt.imshow(im)
print(name)
SaveFigureAsImage('../processed_data/' + name, plt.gcf() )
plt.close("all")
print("----------------------------")
#print(gc.collect())
Подробнее здесь: [url]https://stackoverflow.com/questions/47756176/memory-usage-issues-with-cv2-imread[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия