def _get(img_dir, clf_ckpt_p): out_file = os.path.join(img_dir, OPTIMAL_QS_TXT) clf = load_classifier(clf_ckpt_p) t = timer.TimeAccumulator() opt = {} for i, p in enumerate( cached_listdir_imgs(img_dir, discard_shitty=False).ps): with t.execute(): img = torch.from_numpy(np.array(Image.open(p))).to( pe.DEVICE, non_blocking=True).permute(2, 0, 1) assert img.shape[0] == 3 img = img.unsqueeze(0) img = SymbolTensor(img.long(), L=256).to_norm().get() q = clf.get_q(img) opt[os.path.splitext(os.path.basename(p))[0]] = q if i > 0 and i % 10 == 0: print(i, t.mean_time_spent()) with open(out_file, 'w', newline='') as csvfile: w = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL) w.writerow(['fn', 'q']) for filename, q in sorted(opt.items()): w.writerow([filename, q]) print('Created', out_file)
def unpack(self, img_batch, fixed_first=None): raw = img_batch['raw'].to(pe.DEVICE, non_blocking=True) # uint8 or int16 q = img_batch['q'].to(pe.DEVICE).view(-1) # 1d tensor of floats if fixed_first is not None: raw[0, ...] = fixed_first['raw'] q[0, ...] = fixed_first['q'] q = q - self.config_clf.first_class if self.padding_fac: raw = self.pad(raw) raw = SymbolTensor(raw.long(), L=256) return raw.to_norm(), q
def unpack_batch_pad(self, img_or_imgbatch): raw = img_or_imgbatch['raw'].to(pe.DEVICE, non_blocking=True) # uint8 or int16 q = img_or_imgbatch['q'].to(pe.DEVICE).view(-1) # 1d tensor of floats if len(raw.shape) == 3: raw.unsqueeze_(0) if self.padding_fac: raw = self.pad(raw) q = q - self.config_clf.first_class assert len(raw.shape) == 4 raw = SymbolTensor(raw.long(), L=256) return raw.to_norm(), q