Browse Source

[MNT] add win action and resolve problem of HED under win

pull/1/head
Gao Enhao 2 years ago
parent
commit
b430c11dc7
7 changed files with 23 additions and 15 deletions
  1. +1
    -1
      .github/workflows/build-and-test.yaml
  2. +1
    -1
      docs/Examples/HED.rst
  3. +10
    -4
      examples/hed/bridge.py
  4. +8
    -7
      examples/hed/datasets/get_dataset.py
  5. +1
    -1
      examples/hed/hed.ipynb
  6. +1
    -1
      examples/hed/main.py
  7. +1
    -0
      examples/hed/reasoning/reasoning.py

+ 1
- 1
.github/workflows/build-and-test.yaml View File

@@ -11,7 +11,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, windows-latest]
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v2


+ 1
- 1
docs/Examples/HED.rst View File

@@ -295,5 +295,5 @@ Perform pretraining, training and testing by invoking the ``pretrain``, ``train`
weights_dir = osp.join(log_dir, "weights")
bridge.pretrain("./weights")
bridge.train(train_data, val_data)
bridge.train(train_data, val_data, save_dir=weights_dir)
bridge.test(test_data)

+ 10
- 4
examples/hed/bridge.py View File

@@ -193,7 +193,7 @@ class HedBridge(SimpleBridge):

return data_examples

def train(self, train_data, val_data, segment_size=10, min_len=5, max_len=8):
def train(self, train_data, val_data, segment_size=10, min_len=5, max_len=8, save_dir="./"):
for equation_len in range(min_len, max_len):
print_log(
f"============== equation_len: {equation_len}-{equation_len + 1} ================",
@@ -234,7 +234,9 @@ class HedBridge(SimpleBridge):
seems_good = self.check_rule_quality(rules, val_data, equation_len)
if seems_good:
self.reasoner.kb.learned_rules.update({equation_len: rules})
self.model.save(save_path=f"./weights/eq_len_{equation_len}.pth")
self.model.save(
save_path=os.path.join(save_dir, f"eq_len_{equation_len}.pth")
)
break
else:
if equation_len == min_len:
@@ -242,9 +244,13 @@ class HedBridge(SimpleBridge):
"Learned mapping is: " + str(self.reasoner.idx_to_label),
logger="current",
)
self.model.load(load_path="./weights/pretrain_weights.pth")
self.model.load(
load_path=os.path.join(save_dir, f"pretrain_weights.pth")
)
else:
self.model.load(load_path=f"./weights/eq_len_{equation_len - 1}.pth")
self.model.load(
load_path=os.path.join(save_dir, f"eq_len_{equation_len - 1}.pth")
)
condition_num = 0
print_log("Reload Model and retrain", logger="current")



+ 8
- 7
examples/hed/datasets/get_dataset.py View File

@@ -4,8 +4,8 @@ import pickle
import random
import zipfile
from collections import defaultdict
from PIL import Image

import cv2
import gdown
import numpy as np
from torchvision.transforms import transforms
@@ -32,14 +32,15 @@ def get_pretrain_data(labels, image_size=(28, 28, 1)):
label_path = osp.join(img_dir, label)
img_path_list = os.listdir(label_path)
for img_path in img_path_list:
img = cv2.imread(osp.join(label_path, img_path), cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (image_size[1], image_size[0]))
X.append(np.array(img, dtype=np.float32))
with Image.open(osp.join(label_path, img_path)) as img:
img = img.convert('L')
img = img.resize((image_size[1], image_size[0]))
img_array = np.array(img, dtype=np.float32)
normalized_img = (img_array - 127) / 128.0
X.append(normalized_img)

X = [((img[:, :, np.newaxis] - 127) / 128.0) for img in X]
Y = [img.copy().reshape(image_size[0] * image_size[1] * image_size[2]) for img in X]

X = [transform(img) for img in X]
X = [transform(img[:, :, np.newaxis]) for img in X]
return X, Y




+ 1
- 1
examples/hed/hed.ipynb View File

@@ -396,7 +396,7 @@
"weights_dir = osp.join(log_dir, \"weights\")\n",
"\n",
"bridge.pretrain(weights_dir)\n",
"bridge.train(train_data, val_data)\n",
"bridge.train(train_data, val_data, save_dir=weights_dir)\n",
"bridge.test(test_data)"
]
}


+ 1
- 1
examples/hed/main.py View File

@@ -95,7 +95,7 @@ def main():
weights_dir = osp.join(log_dir, "weights")

bridge.pretrain(weights_dir)
bridge.train(train_data, val_data)
bridge.train(train_data, val_data, save_dir=weights_dir)
bridge.test(test_data)




+ 1
- 0
examples/hed/reasoning/reasoning.py View File

@@ -10,6 +10,7 @@ CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))

class HedKB(PrologKB):
def __init__(self, pseudo_label_list=[1, 0, "+", "="], pl_file=os.path.join(CURRENT_DIR, "learn_add.pl")):
pl_file = pl_file.replace("\\", "/")
super().__init__(pseudo_label_list, pl_file)
self.learned_rules = {}



Loading…
Cancel
Save