def get_mean_and_var(self): imgs = [] for t in self.train: imgs.extend(t[0]) channel = 3 x_tot = np.zeros(channel) x2_tot = np.zeros(channel) for img in tqdm(imgs): x = T.ToTensor()(read_image(img)).view(3, -1) x_tot += x.mean(dim=1).numpy() x2_tot += (x**2).mean(dim=1).numpy() channel_avr = x_tot / len(imgs) channel_std = np.sqrt(x2_tot / len(imgs) - channel_avr**2) print(channel_avr, channel_std)
def attr_predict(model, transform_test, use_gpu): import psycopg2 from PIL import Image # set-up a postgres connection conn = psycopg2.connect(database='ng', user='******', password='******', host='146.148.89.5', port=5432) dbcur = conn.cursor() print("connection successful") try: sql = dbcur.mogrify(""" select mgid, ctype, pid, camid, imagepaths from mmir_ground where ctype='image' or ctype='video' """) dbcur.execute(sql) with torch.no_grad(): model.eval() rows = dbcur.fetchall() for row in rows: mgid, ctype, pid, camid, img_paths = row imgs = [] sz = 0 for img_path in img_paths: img = read_image( "/homes/ksolaima/scratch1/yuange250_video_pedestrian_attributes_recognition-master" + img_path[1:]) img = transform_test(img) img = img.unsqueeze(0) imgs.append(img) imgs = torch.cat(imgs, dim=0) imgs = imgs.unsqueeze( 0) # to match the group testing of theirs -_- if use_gpu: imgs = imgs.cuda() outputs = model(imgs) # outputs = [torch.mean(out, 0).view(1, -1) for out in outputs] # no need predicted_attrs = [] for i in range(len(outputs)): outs = outputs[i].cpu().numpy() # print(outs.shape) predicted_attrs.append( np.argmax(outs, 1)[0] ) # [0] because of the unsqueezing of grouping testing print(predicted_attrs) sql = dbcur.mogrify( """ INSERT INTO mmir_predicted ( mgid, ctype, pid, camid, ubc, lbc, gender, imagepaths ) VALUES (%s, %s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING;""", (str(mgid), ctype, str(pid), str(camid), str(predicted_attrs[0]), str(predicted_attrs[1]), str(predicted_attrs[2]), img_paths)) dbcur.execute(sql) conn.commit() except (Exception, psycopg2.Error) as error: print(error) finally: #closing database connection. if (conn): dbcur.close() conn.close() print("PostgreSQL connection is closed")