AssemblyAI-Community / MinImagen

MinImagen: A minimal implementation of the Imagen text-to-image model
https://assemblyai-examples.github.io/MinImagen/
MIT License
296 stars 54 forks source link

object of type 'NoneType' has no len() #19

Closed sadath-12 closed 1 year ago

sadath-12 commented 1 year ago

With this provided training script


from datetime import datetime

import torch.utils.data
from torch import optim

from minimagen.Imagen import Imagen
from minimagen.Unet import Unet, Base, Super, BaseTest, SuperTest
from minimagen.generate import load_minimagen, load_params
from minimagen.t5 import get_encoded_dim
from minimagen.training import get_minimagen_parser, ConceptualCaptions, get_minimagen_dl_opts, \
    create_directory, get_model_params, get_model_size, save_training_info, get_default_args, MinimagenTrain, \
    load_restart_training_parameters, load_testing_parameters

# Get device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Command line argument parser
parser = get_minimagen_parser()
# args = parser.parse_args()
args = parser.parse_known_args()

# Create training directory
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
dir_path = f"./training_{timestamp}"
training_dir = create_directory(dir_path)

# Replace some cmd line args to lower computational load.
args = load_testing_parameters(args)

# Load subset of Conceptual Captions dataset.
train_dataset, valid_dataset = ConceptualCaptions(args, smalldata=True)

# Create dataloaders
dl_opts = {**get_minimagen_dl_opts(device), 'batch_size': args.BATCH_SIZE, 'num_workers': args.NUM_WORKERS}
train_dataloader = torch.utils.data.DataLoader(train_dataset, **dl_opts)
valid_dataloader = torch.utils.data.DataLoader(valid_dataset, **dl_opts)

# Use small U-Nets to lower computational load.
unets_params = [get_default_args(BaseTest), get_default_args(SuperTest)]
unets = [Unet(**unet_params).to(device) for unet_params in unets_params]

# Specify MinImagen parameters
imagen_params = dict(
    image_sizes=(int(args.IMG_SIDE_LEN / 2), args.IMG_SIDE_LEN),
    timesteps=args.TIMESTEPS,
    cond_drop_prob=0.15,
    text_encoder_name=args.T5_NAME
)

# Create MinImagen from UNets with specified imagen parameters
imagen = Imagen(unets=unets, **imagen_params).to(device)

# Fill in unspecified arguments with defaults to record complete config (parameters) file
unets_params = [{**get_default_args(Unet), **i} for i in unets_params]
imagen_params = {**get_default_args(Imagen), **imagen_params}

# Get the size of the Imagen model in megabytes
model_size_MB = get_model_size(imagen)

# Save all training info (config files, model size, etc.)
save_training_info(args, timestamp, unets_params, imagen_params, model_size_MB, training_dir)

# Create optimizer
optimizer = optim.Adam(imagen.parameters(), lr=args.OPTIM_LR)

# Train the MinImagen instance
MinimagenTrain(timestamp, args, unets, imagen, train_dataloader, valid_dataloader, training_dir, optimizer, timeout=30)

from argparse import ArgumentParser
from minimagen.generate import load_minimagen, sample_and_save

# Command line argument parser
parser = ArgumentParser()
parser.add_argument("-d", "--TRAINING_DIRECTORY", dest="TRAINING_DIRECTORY", help="Training directory to use for inference", type=str)
args = parser.parse_args()

# Specify the caption(s) to generate images for
captions = ['a happy dog']

# Use `sample_and_save` to generate and save the iamges
sample_and_save(captions, training_directory=args.TRAINING_DIRECTORY)

# Alternatively, rather than specifying a Training Directory, you can input just a MinImagen instance to use for image generation.
# In this case, information about the MinImagen instance used to generate the images will not be saved.
minimagen = load_minimagen(args.TRAINING_DIRECTORY)
sample_and_save(captions, minimagen=minimagen)  ```  

I get error ``` usage: ipykernel_launcher.py [-h] [-p PARAMETERS] [-n NUM_WORKERS]
                             [-b BATCH_SIZE] [-mw MAX_NUM_WORDS]
                             [-s IMG_SIDE_LEN] [-e EPOCHS] [-t5 T5_NAME]
                             [-f TRAIN_VALID_FRAC] [-t TIMESTEPS]
                             [-lr OPTIM_LR] [-ai ACCUM_ITER] [-cn CHCKPT_NUM]
                             [-vn VALID_NUM] [-rd RESTART_DIRECTORY] [-test]
ipykernel_launcher.py: error: argument -f/--TRAIN_VALID_FRAC: invalid float value: '/root/.local/share/jupyter/runtime/kernel-7d986ed8-b1e3-4cc4-b594-d8d9901e471a.json'
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File /opt/conda/lib/python3.10/argparse.py:2488, in ArgumentParser._get_value(self, action, arg_string)
   2487 try:
-> 2488     result = type_func(arg_string)
   2490 # ArgumentTypeErrors indicate errors

ValueError: could not convert string to float: '/root/.local/share/jupyter/runtime/kernel-7d986ed8-b1e3-4cc4-b594-d8d9901e471a.json'

During handling of the above exception, another exception occurred:

ArgumentError                             Traceback (most recent call last)
File /opt/conda/lib/python3.10/argparse.py:1859, in ArgumentParser.parse_known_args(self, args, namespace)
   1858 try:
-> 1859     namespace, args = self._parse_known_args(args, namespace)
   1860 except ArgumentError:

File /opt/conda/lib/python3.10/argparse.py:2072, in ArgumentParser._parse_known_args(self, arg_strings, namespace)
   2071     # consume the next optional and any arguments for it
-> 2072     start_index = consume_optional(start_index)
   2074 # consume any positionals following the last Optional

File /opt/conda/lib/python3.10/argparse.py:2012, in ArgumentParser._parse_known_args.<locals>.consume_optional(start_index)
   2011 for action, args, option_string in action_tuples:
-> 2012     take_action(action, args, option_string)
   2013 return stop

File /opt/conda/lib/python3.10/argparse.py:1920, in ArgumentParser._parse_known_args.<locals>.take_action(action, argument_strings, option_string)
   1919 seen_actions.add(action)
-> 1920 argument_values = self._get_values(action, argument_strings)
   1922 # error if this argument is not allowed with other previously
   1923 # seen arguments, assuming that actions that use the default
   1924 # value don't really count as "present"

File /opt/conda/lib/python3.10/argparse.py:2455, in ArgumentParser._get_values(self, action, arg_strings)
   2454 arg_string, = arg_strings
-> 2455 value = self._get_value(action, arg_string)
   2456 self._check_value(action, value)

File /opt/conda/lib/python3.10/argparse.py:2501, in ArgumentParser._get_value(self, action, arg_string)
   2500     msg = _('invalid %(type)s value: %(value)r')
-> 2501     raise ArgumentError(action, msg % args)
   2503 # return the converted value

ArgumentError: argument -f/--TRAIN_VALID_FRAC: invalid float value: '/root/.local/share/jupyter/runtime/kernel-7d986ed8-b1e3-4cc4-b594-d8d9901e471a.json'

During handling of the above exception, another exception occurred:

SystemExit                                Traceback (most recent call last)
    [... skipping hidden 1 frame]

Cell In[4], line 21
     20 # args = parser.parse_args()
---> 21 args = parser.parse_known_args()
     22 args=args[0]

File /opt/conda/lib/python3.10/argparse.py:1862, in ArgumentParser.parse_known_args(self, args, namespace)
   1861         err = _sys.exc_info()[1]
-> 1862         self.error(str(err))
   1863 else:

File /opt/conda/lib/python3.10/argparse.py:2587, in ArgumentParser.error(self, message)
   2586 args = {'prog': self.prog, 'message': message}
-> 2587 self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

File /opt/conda/lib/python3.10/argparse.py:2574, in ArgumentParser.exit(self, status, message)
   2573     self._print_message(message, _sys.stderr)
-> 2574 _sys.exit(status)

SystemExit: 2

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
    [... skipping hidden 1 frame]

File /opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py:2095, in InteractiveShell.showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
   2092 if exception_only:
   2093     stb = ['An exception has occurred, use %tb to see '
   2094            'the full traceback.\n']
-> 2095     stb.extend(self.InteractiveTB.get_exception_only(etype,
   2096                                                      value))
   2097 else:
   2098     try:
   2099         # Exception classes can customise their traceback - we
   2100         # use this in IPython.parallel for exceptions occurring
   2101         # in the engines. This should return a list of strings.

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:696, in ListTB.get_exception_only(self, etype, value)
    688 def get_exception_only(self, etype, value):
    689     """Only print the exception type and message, without a traceback.
    690 
    691     Parameters
   (...)
    694     value : exception value
    695     """
--> 696     return ListTB.structured_traceback(self, etype, value)

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:559, in ListTB.structured_traceback(self, etype, evalue, etb, tb_offset, context)
    556     chained_exc_ids.add(id(exception[1]))
    557     chained_exceptions_tb_offset = 0
    558     out_list = (
--> 559         self.structured_traceback(
    560             etype,
    561             evalue,
    562             (etb, chained_exc_ids),  # type: ignore
    563             chained_exceptions_tb_offset,
    564             context,
    565         )
    566         + chained_exception_message
    567         + out_list)
    569 return out_list

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:1396, in AutoFormattedTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1394 else:
   1395     self.tb = etb
-> 1396 return FormattedTB.structured_traceback(
   1397     self, etype, evalue, etb, tb_offset, number_of_lines_of_context
   1398 )

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:1287, in FormattedTB.structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1284 mode = self.mode
   1285 if mode in self.verbose_modes:
   1286     # Verbose modes need a full traceback
-> 1287     return VerboseTB.structured_traceback(
   1288         self, etype, value, tb, tb_offset, number_of_lines_of_context
   1289     )
   1290 elif mode == 'Minimal':
   1291     return ListTB.get_exception_only(self, etype, value)

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:1140, in VerboseTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1131 def structured_traceback(
   1132     self,
   1133     etype: type,
   (...)
   1137     number_of_lines_of_context: int = 5,
   1138 ):
   1139     """Return a nice text document describing the traceback."""
-> 1140     formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
   1141                                                            tb_offset)
   1143     colors = self.Colors  # just a shorthand + quicker name lookup
   1144     colorsnormal = colors.Normal  # used a lot

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:1030, in VerboseTB.format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)
   1027 assert isinstance(tb_offset, int)
   1028 head = self.prepare_header(str(etype), self.long_header)
   1029 records = (
-> 1030     self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
   1031 )
   1033 frames = []
   1034 skipped = 0

File /opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py:1098, in VerboseTB.get_records(self, etb, number_of_lines_of_context, tb_offset)
   1096 while cf is not None:
   1097     try:
-> 1098         mod = inspect.getmodule(cf.tb_frame)
   1099         if mod is not None:
   1100             mod_name = mod.__name__

AttributeError: 'tuple' object has no attribute 'tb_frame' ```

To Reproduce the same error Run the following script in google collab or kaggle 
sadath-12 commented 1 year ago

solved**