Git Product home page Git Product logo

video-game-behavioural-cloning's People

Contributors

joonaspu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

video-game-behavioural-cloning's Issues

Cannot initiate training for vizdoom or anything else

some debugging attempts and the error i get:

conv3: 1536
vizdoom_deathmatch
<module 'json' from 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p0\\lib\\json\\__init__.py'>
png
Traceback (most recent call last):
  File "c:\Users\Lenovo2\Documents\Python Projects\! Behavioral Cloning from Video (of Gameplay)\train.py", line 79, in <module>
    gen = MultiprocessAtariDataLoader(dataloader_args, workers)
  File "c:\Users\Lenovo2\Documents\Python Projects\! Behavioral Cloning from Video (of Gameplay)\utils\atari_dataloader.py", line 520, in __init__
    loader = AtariDataLoader(**dataloader_args)
  File "c:\Users\Lenovo2\Documents\Python Projects\! Behavioral Cloning from Video (of Gameplay)\utils\atari_dataloader.py", line 143, in __init__
    data = self.get_batch(batch)
  File "c:\Users\Lenovo2\Documents\Python Projects\! Behavioral Cloning from Video (of Gameplay)\utils\atari_dataloader.py", line 402, in get_batch
    for sample in samples:
TypeError: 'int' object is not iterable

I modified the code (which I will paste below) to allow it to be run without the shell files and you can set the arguments manually in the code. However, I cannot get past this step and have tried pretty much everything in attempt to fix it. I know you aren't developing it anymore but if possible to try to see what the issue could be and how to fix it. I apologize for any inconvenience.
Thanks.

Modified code (no modifications to the code itself except how the arguments are defined)

from argparse import ArgumentParser
import json

import torch
import torch.nn as nn
import torch.nn.functional as F

import numpy as np

from utils.atari_dataloader import MultiprocessAtariDataLoader
from utils.atari_head_dataloader import MultiprocessAtariHeadDataLoader

from utils.networks import Mnih2015

if __name__ == "__main__":
    input_directory = "! Behavioral Cloning from Video (of Gameplay)/recording"  # Path to directory with recorded gameplay
    game = "vizdoom_deathmatch"  # Name of the game to use for training
    model = "! Behavioral Cloning from Video (of Gameplay)/model"  # Path of the file where model will be saved (optional)
    actions = 18  # Number of actions
    framestack = 3  # Number of frames to stack
    merge = False  # Merge stacked frames into one image
    width = 80  # Width of the image
    height = 60  # Height of the image
    batch_size = 32  # Batch size
    epochs = 30  # Number of epochs to train
    workers = 4  # Number of worker processes for the dataloader
    l2 = 0.00001  # L2 regularization weight
    percentile = None  # The top q-percentile of samples to use for training (optional)
    top_n = None  # The top n number of samples to use for training (optional)
    save_frequency = 1  # Number of epochs between checkpoints
    augment = True  # Use image augmentation
    preload = True  # Preload image data to memory
    atari_head = False # Use the Atari-HEAD dataloader
    action_delay = 0  # How many frames to delay the actions by
    use_cuda = False  # Use CUDA if available
    dataset_is_json = True  # Dataset is stored as JSON
    fileformat = "png"  # Postfix of the image files to be loaded

    if use_cuda:
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    model = Mnih2015(
        (width, height),
        3 if merge else 3*framestack,
        actions
    ).to(device)
    optimizer = torch.optim.Adam(model.parameters(), weight_decay=l2)

    dataloader_args = {
        "directory": input_directory,
        "game": game,
        "stack": framestack,
        "batch_size": batch_size,
        "size": (width, height),
        "percentile": percentile,
        "top_n": top_n,
        "augment": augment,
        "preload": preload,
        "merge": merge,
        "json": json,
        "action_delay": action_delay,
        "fileformat": fileformat
    }

    # Note: if new dataloader arguments are added, make sure they work with
    #       both loaders, or if they don't, remove them with 'del' below
    if atari_head:
        del dataloader_args["game"]
        del dataloader_args["json"]
        del dataloader_args["fileformat"]
        gen = MultiprocessAtariHeadDataLoader(dataloader_args, workers)
    else:      
        print(dataloader_args["game"])
        print(dataloader_args["json"])
        print(dataloader_args["fileformat"])
        gen = MultiprocessAtariDataLoader(dataloader_args, workers)
    shape = gen.shape

    history = dict()
    history["loss"] = []
    history["accuracy"] = []

    for epoch in range(1, epochs + 1):
        print("Starting epoch {}".format(epoch))
        model.train()
        start = perf_counter()

        # Accuracy
        correct = 0
        total = 0

        # Loss
        loss_sum = 0
        loss_num = 0

        for batch_size, data in enumerate(gen):
            # Convert data to correct format
            x = torch.Tensor(np.swapaxes(data[0], 1, 3)).to(device) / 255
            if json:
                # Drop unnecessary axis
                y = torch.Tensor(data[1]).to(device)[:, 0, :]
            else:
                y = torch.argmax(torch.Tensor(data[1]).to(device), 1).long()

            optimizer.zero_grad()

            # Get model output
            output = model(x)

            # Calculate loss
            if json:
                loss = F.binary_cross_entropy_with_logits(output, y)
            else:
                loss = F.cross_entropy(output, y)

            # Add loss to epoch statistics
            loss_sum += loss
            loss_num += 1

            # Calculate accuracy and add to epoch statistics
            if json:
                correct += 0 # TODO
            else:
                correct += output.argmax(1).eq(y).sum()

            total += len(y)
            
            # Backpropagate loss
            loss.backward()
            optimizer.step()

            # Print statistics
            if batch_size % 100 == 0:
                end = perf_counter()
                accuracy = float(correct) / float(total)
                loss = loss_sum / loss_num
                print("Epoch {} - {}/{}: loss: {}, acc: {} ({} s/batch_size)".format(
                    epoch,
                    batch_size,
                    len(gen),
                    loss,
                    accuracy,
                    (end - start) / 100)
                )
                start = perf_counter()

        # Save statistics
        accuracy = float(correct) / float(total)
        loss = loss_sum / loss_num

        history["accuracy"].append(float(accuracy))
        history["loss"].append(float(loss))

        with open(model + "-history.json", "w") as f:
            json.dump(history, f)

        # Save model
        if model is not None and epoch % save_freq == 0:
            filename = "{}_{}.pt".format(model, epoch)
            print("Saving {}".format(filename))
            torch.save(model, filename)

        print("Finished epoch {}".format(epoch))
    
    gen.stop()

Problem during model training

Hi! I have successfully recorded the data from the game and converted it. But when I started training the model, the following error appeared:
Traceback (most recent call last):
File "C:\video-game-behavioural-cloning-master\train.py", line 101, in
gen = Multi process Atari Data Loader(dataloader_args, args.workers)
File "C:\video-game-behavioural-cloning-master\utils\atari_dataloader.py", line 524, in init
self.shape = loader._get_image(0, 0).shape
File "C:\video-game-behavioural-cloning-master\utils\atari_dataloader.py", line 184, in _get_image
traj_name = self.all_trajs[traj]
IndexError: list index out of range
I tried to solve this error on my own, but I'm new to programming and I couldn't do it.
I hope you will find time to help me)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.