Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: drsm79/modulair
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: IBMDeveloperUK/Modulair
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 9 commits
  • 5 files changed
  • 3 contributors

Commits on Sep 15, 2020

  1. add boxes

    Simon Metson committed Sep 15, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    67efa8f View commit details
  2. fix merge

    Matt Hamilton committed Sep 15, 2020
    Copy the full SHA
    0862271 View commit details
  3. Create tfrecords

    Matt Hamilton committed Sep 15, 2020
    Copy the full SHA
    21c926f View commit details

Commits on Sep 16, 2020

  1. store files in directories

    Matt Hamilton committed Sep 16, 2020
    Copy the full SHA
    d667039 View commit details
  2. Create README.md

    hammertoe authored Sep 16, 2020
    Copy the full SHA
    a5e8b76 View commit details
  3. Create two files, one for training, one for test

    Matt Hamilton committed Sep 16, 2020
    Copy the full SHA
    a4e1a25 View commit details
  4. Copy the full SHA
    1cb46cb View commit details
  5. Add example modules image

    Matt Hamilton committed Sep 16, 2020
    Copy the full SHA
    5cde6b7 View commit details
  6. actually add the images

    Matt Hamilton committed Sep 16, 2020
    Copy the full SHA
    162dac2 View commit details
Showing with 77 additions and 18 deletions.
  1. +9 −0 README.md
  2. BIN _images/modules_668a1a57.jpg
  3. +2 −2 collect.py
  4. +63 −15 randomise.py
  5. +3 −1 requirements.txt
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# modulair

These are scripts to fetch images of modules from modular sythesizers and format them for training in an object detection model.

![Example training image of synth modules](_images/modules_668a1a57.jpg)

You can read all about this mini-project here:

https://dev.to/hammertoe/using-machine-learning-to-catalog-modular-synthesizers-co2
Binary file added _images/modules_668a1a57.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions collect.py
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ def save_images(data):
print(module["id"], module["image"])
r = requests.get(module["image"], stream=True)
r.raise_for_status()
with open(module["image"].split('/')[-1], 'wb') as f:
with open("modules/" + module["image"].split('/')[-1], 'wb') as f:
for chunk in r:
f.write(chunk)

@@ -77,7 +77,7 @@ def save_images(data):
try:
results = get_search(next)
data = parse_results(results)
with open(f'modules_page_{next}.json', 'w') as f:
with open(f'modules/modules_page_{next}.json', 'w') as f:
json.dump(data, f)
save_images(data)
time.sleep(2)
78 changes: 63 additions & 15 deletions randomise.py
Original file line number Diff line number Diff line change
@@ -3,9 +3,13 @@
import hashlib
import pathlib
import random
from tqdm import tqdm


from PIL import Image

import tensorflow.compat.v1 as tf
from object_detection.utils import dataset_util, label_map_util

def join_modules(modules):
image = False
@@ -36,14 +40,14 @@ def concat_images(height, width, images):
return dst, coords


def pick_modules_from_dir(data_dir='data', count=1):
def pick_modules_from_dir(data_dir='modules', count=1):
data = list(pathlib.Path(data_dir).glob('./*.jpg'))
result = []
i = 0
while i < count:

for _ in range(count):
p = random.choice(data)
result.append(Image.open(p.as_posix()))
i += 1

return result


@@ -58,29 +62,73 @@ def pick_modules_from_data(jsonfile='data/modules_page_1.json', count=1):
if p["id"] not in result:
if '1u' not in p["name"].lower():
result[p["id"]] = p
image = pathlib.Path(f'data/{p["image"].split("/")[-1]}')
image = pathlib.Path(f'modules/{p["image"].split("/")[-1]}')
if image.exists():
result[p["id"]]["image"] = Image.open(image.as_posix())
i += 1
return result


if __name__ == "__main__":
modules = pick_modules_from_data(count=5)
def gen_tfrecord():
page = random.randint(1, 5)
jsonfile=f'modules/modules_page_{page}.json'
modules = pick_modules_from_data(count=5, jsonfile=jsonfile)
image, all_coords = join_modules([m["image"] for m in modules.values()])

hash = hashlib.sha256()
for i in [m.encode('utf-8') for m in modules]:
hash.update(bytes(i))
h = hash.hexdigest()[:8]

filename = f'modules_{h}.jpg'
filename = f'composites/modules_{h}.jpg'
image.save(filename)
to_save = []

xmins, xmaxs = [], []
ymins, ymaxs = [], []
classes_text, classes = [], []

for i, d in enumerate(modules.values()):
data = {k: v for k, v in d.items() if k != 'image'}
data['x_min'], data['x_max'] = all_coords[i]
to_save.append(data)
with open(f'modules_{h}.json', 'w') as f:
json.dump(to_save, f, indent=2, sort_keys=True)
x_min, x_max = all_coords[i]

xmins.append(x_min)
xmaxs.append(x_max)
ymins.append(0)
ymaxs.append(image.height)
classes_text.append(data['name'].encode('utf-8'))
classes.append(int(data['id']))

tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(image.height),
'image/width': dataset_util.int64_feature(image.width),
'image/filename': dataset_util.bytes_feature(filename.encode('utf-8')),
'image/source_id': dataset_util.bytes_feature(filename.encode('utf-8')),
'image/encoded': dataset_util.bytes_feature(open(filename, "rb").read()),
'image/format': dataset_util.bytes_feature( b'jpg'),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))

return tf_example


def write_records(filename, num):

writer = tf.python_io.TFRecordWriter(filename)

for i in tqdm(range(num)):
tf_example = gen_tfrecord()
writer.write(tf_example.SerializeToString())
writer.close()

print(f'Successfully created the TFRecord file with {num} records: {filename}')


if __name__ == "__main__":

write_records("train.record", 1000)
write_records("test.record", 100)

4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
beautifulsoup4==4.9.1
Pillow==7.2.0
requests==2.24.0
tensorflow
tensorflow
tqdm