I'm doing an experiment with image generation, but my script outputs a binary file, how can I train a model using llama2.c?
win10ogod opened this issue · 0 comments
win10ogod commented
I'm doing an experiment with image generation, but my script outputs a binary file, how can I train a model using llama2.c?
import cv2
import numpy as np
import os
def image_to_binary(image_path, binary_path):
# Read the image
image = cv2.imread(image_path)
# Convert the image to np.uint16 type
image_uint16 = image.astype(np.uint16)
# Get image shape
height, width, channels = image.shape
# Save the shape information and the image to a binary file
with open(binary_path, 'wb') as binary_file:
# Write the shape information as 3 uint16 numbers
binary_file.write(np.uint16(height).tobytes())
binary_file.write(np.uint16(width).tobytes())
binary_file.write(np.uint16(channels).tobytes())
# Write the image data
binary_file.write(image_uint16.tobytes())
def binary_to_image(binary_path, output_image_path):
# Read the binary file
with open(binary_path, 'rb') as binary_file:
# Read the shape information
height = np.frombuffer(binary_file.read(2), dtype=np.uint16)[0]
width = np.frombuffer(binary_file.read(2), dtype=np.uint16)[0]
channels = np.frombuffer(binary_file.read(2), dtype=np.uint16)[0]
# Read the image data
image_data = binary_file.read()
# Convert the binary data back to np.uint16 type array
image_uint16 = np.frombuffer(image_data, dtype=np.uint16)
# Reshape the array to the original image shape
image_array = image_uint16.reshape((height, width, channels))
# Convert the array to an 8-bit image before saving (if needed)
image_array_8bit = image_array.astype(np.uint8)
# Save the image
cv2.imwrite(output_image_path, image_array_8bit)
def batch_process(input_folder, output_folder_bin, output_folder_images):
# Ensure the output folders exist
if not os.path.exists(output_folder_bin):
os.makedirs(output_folder_bin)
if not os.path.exists(output_folder_images):
os.makedirs(output_folder_images)
# Iterate through all image files in the folder
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.jpg', '.png', '.jpeg')): # Handle common image formats
print(f"Processing {filename}...")
image_path = os.path.join(input_folder, filename)
base_filename = os.path.splitext(filename)[0]
binary_path = os.path.join(output_folder_bin, base_filename + '.bin')
output_image_path = os.path.join(output_folder_images, filename)
# Image to binary
image_to_binary(image_path, binary_path)
# Binary to image
binary_to_image(binary_path, output_image_path)
# Example use
input_folder = 'D:/llama2.c-master/1/images'
output_folder_bin = 'D:/llama2.c-master/1/bin'
output_folder_images = 'D:/llama2.c-master/1/imagesout'
batch_process(input_folder, output_folder_bin, output_folder_images)
@karpathy Can you help?