没有合适的资源?快使用搜索试试~ 我知道了~
源代码文档1
需积分: 0 0 下载量 39 浏览量
2022-08-08
18:06:22
上传
评论
收藏 59KB DOCX 举报
温馨提示
试读
39页
源代码文档1
资源详情
资源评论
资源推荐
软
件
名
称
strdsty
源
程
序
软件名称 stranded density 1.0
1
训练数据获取
from scipy.io import loadmat
import glob
import cv2
import os
from sklearn.neighbors import NearestNeighbors
import numpy as np
import matplotlib.pyplot as plt
# 生成密度图所使用高斯核大小
Gauss_ksize = 3
class MallDataset(object):
def __init__(self):
self.filenames = sorted(glob.glob('../data/mall_dataset/frames/*.jpg'), key=lambda x:
int(x[-8:-4]))
def get_train_num(self):
return int(len(self.filenames) * 0.8)
def get_valid_num(self):
return len(self.filenames) - int(len(self.filenames) * 0.8)
def get_annotation(self):
mat_annotation = loadmat('../data/mall_dataset/mall_gt.mat')
count_data, position_data = mat_annotation['count'], mat_annotation['frame'][0]
return count_data, position_data
def get_pixels(self, img, img_index, positions, size):
h, w = img.shape[:-1]
proportion_h, proportion_w = size / h, size / w # 输入层需求与当前图片大小
对比
pixels = np.zeros((size, size))
for p in positions[img_index][0][0][0]:
# 取出每个人的坐标
now_x, now_y = int(p[0] * proportion_w), int(p[1] * proportion_h) # 按照
输入层要求调整坐标位置
if now_x >= size or now_y >= size:
# 越界则向下取整
print("Sorry skip the point, its index of all is {}".format(img_index))
else:
pixels[now_y, now_x] += 1
pixels = cv2.GaussianBlur(pixels, (15, 15), 0)
return pixels
软件名称 stranded density 1.0
2
def get_img_data(self, index, size):
_, positions = self.get_annotation()
img = cv2.imread(self.filenames[index])
density_map = np.expand_dims(self.get_pixels(img, index, positions, size // 4), axi
s=-1)
img = cv2.resize(img, (size, size)) / 255.
return img, density_map
def gen_train(self, batch_size, size):
_, position = self.get_annotation()
index_all = list(range(int(len(self.filenames) * 0.8))) # 取出所有训练数据下标,默
认数据的前 80%为训练集
i, n = 0, len(index_all)
if batch_size > n:
raise Exception('Batch size {} is larger than the number of dataset {}!'.forma
t(batch_size, n))
while True:
if i + batch_size >= n:
np.random.shuffle(index_all)
i = 0
continue
batch_x, batch_y = [], []
for j in range(i, i + batch_size):
x, y = self.get_img_data(index_all[j], size)
batch_x.append(x)
batch_y.append(y)
i += batch_size
yield np.array(batch_x), np.array(batch_y)
def gen_valid(self, batch_size, size):
_, position = self.get_annotation()
index_all = list(range(int(len(self.filenames) * 0.8), len(self.filenames)))
i, n = 0, len(index_all)
if batch_size > n:
raise Exception('Batch size {} is larger than the number of dataset {}!'.forma
t(batch_size, n))
while True:
if i + batch_size >= n:
np.random.shuffle(index_all)
i = 0
continue
软件名称 stranded density 1.0
3
batch_x, batch_y = [], []
for j in range(i, i + batch_size):
x, y = self.get_img_data(index_all[j], size)
batch_x.append(x)
batch_y.append(y)
i += batch_size
yield np.array(batch_x), np.array(batch_y)
def gen_all(self, pic_size):
x_data = []
y_data = []
for i in range(len(self.filenames)):
image, map_ = self.get_img_data(i, pic_size)
x_data.append(image)
y_data.append(map_)
x_data, y_data = np.array(x_data), np.array(y_data)
return x_data, y_data
class ShanghaitechDataset(object):
def __init__(self, part='A'):
if part == 'A':
self.folder = '../data/ShanghaiTech/part_A/'
else:
self.folder = '../data/ShanghaiTech/part_B/'
def get_annotation(self, folder, index):
mat_data = loadmat(folder + 'ground_truth/GT_IMG_{}.mat'.format(index))
positions, count = mat_data['image_info'][0][0][0][0][0], mat_data['image_info'][0][0]
[0][0][1][0][0]
return positions, count
def get_pixels(self, folder, img, img_index, size):
positions, _ = self.get_annotation(folder, img_index)
h, w = img.shape[0], img.shape[1]
proportion_h, proportion_w = size / h, size / w # 输入层需求与当前图片大小
对比
pixels = np.zeros((size, size))
for p in positions:
# 取出每个人的坐标
now_x, now_y = int(p[0] * proportion_w), int(p[1] * proportion_h) # 按照
输入层要求调整坐标位置
if now_x >= size or now_y >= size:
# 越界则向下取整
软件名称 stranded density 1.0
4
pass
# print("Sorry skip the point, image index of all is {}".format(img_inde
x))
else:
pixels[now_y, now_x] += 1
pixels = cv2.GaussianBlur(pixels, (15, 15), 0)
return pixels
def gen_train(self, batch_size, size):
folder = self.folder + 'train_data/'
index_all = [i+1 for i in range(len(glob.glob(folder + 'images/*.jpg')))]
i, n = 0, len(index_all)
if batch_size > n:
raise Exception('Batch size {} is larger than the number of dataset {}!'.forma
t(batch_size, n))
while True:
if i + batch_size >= n:
np.random.shuffle(index_all)
i = 0
continue
batch_x, batch_y = [], []
for j in range(i, i + batch_size):
img = cv2.imread(folder + 'images/IMG_{}.jpg'.format(index_all[j]))
density = np.expand_dims(self.get_pixels(folder, img, index_all[j], size //
4), axis=-1)
img = cv2.resize(img, (size, size)) / 255.
batch_x.append(img)
batch_y.append(density)
i += batch_size
yield np.array(batch_x), np.array(batch_y)
def gen_valid(self, batch_size, size):
folder = self.folder + 'test_data/'
index_all = [i + 1 for i in range(len(glob.glob(folder + 'images/*.jpg')))]
i, n = 0, len(index_all)
if batch_size > n:
raise Exception('Batch size {} is larger than the number of dataset {}!'.forma
t(batch_size, n))
while True:
if i + batch_size >= n:
np.random.shuffle(index_all)
i = 0
剩余38页未读,继续阅读
黄涵奕
- 粉丝: 72
- 资源: 328
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0