def test_saving_with_serializers(tmpdir): checkpoint_file = os.path.join(tmpdir, 'tmp.ckpt') class CustomModel(Task): def __init__(self): super().__init__(model=torch.nn.Linear(1, 1), loss_fn=torch.nn.MSELoss()) serializer = Labels(["a", "b"]) model = CustomModel() trainer = Trainer(fast_dev_run=True) data_pipeline = DataPipeline(DefaultPreprocess(), serializer=serializer) data_pipeline.initialize() model.data_pipeline = data_pipeline assert isinstance(model.preprocess, DefaultPreprocess) dummy_data = DataLoader( list( zip(torch.arange(10, dtype=torch.float), torch.arange(10, dtype=torch.float)))) trainer.fit(model, train_dataloader=dummy_data) trainer.save_checkpoint(checkpoint_file) model = CustomModel.load_from_checkpoint(checkpoint_file) assert isinstance(model.preprocess._data_pipeline_state, DataPipelineState) assert model.preprocess._data_pipeline_state._state[ ClassificationState] == ClassificationState(['a', 'b'])
""" The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct cultural and ethnic identity of the Normans emerged initially in the first half of the 10th century, and it continued to evolve over the succeeding centuries. """, """ The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct cultural and ethnic identity of the Normans emerged initially in the first half of the 10th century, and it continued to evolve over the succeeding centuries. """, ], "question": ["When were the Normans in Normandy?", "In what country is Normandy located?"], }, batch_size=4, ) predictions = trainer.predict(model, datamodule=datamodule) print(predictions) # 5. Save the model! trainer.save_checkpoint("question_answering_on_sqaud_v2.pt")
# 3. Create the trainer and finetune the model trainer = Trainer(max_epochs=3) trainer.finetune(model, datamodule=datamodule) # 4. Summarize some text! predictions = model.predict(""" Camilla bought a box of mangoes with a Brixton £10 note, introduced last year to try to keep the money of local people within the community.The couple were surrounded by shoppers as they walked along Electric Avenue. They came to Brixton to see work which has started to revitalise the borough. It was Charles' first visit to the area since 1996, when he was accompanied by the former South African president Nelson Mandela.Greengrocer Derek Chong, who has run a stall on Electric Avenue for 20 years, said Camilla had been ""nice and pleasant"" when she purchased the fruit. ""She asked me what was nice, what would I recommend, and I said we've got some nice mangoes. She asked me were they ripe and I said yes - they're from the Dominican Republic."" Mr Chong is one of 170 local retailers who accept the Brixton Pound. Customers exchange traditional pound coins for Brixton Pounds and then spend them at the market or in participating shops. During the visit, Prince Charles spent time talking to youth worker Marcus West, who works with children nearby on an estate off Coldharbour Lane. Mr West said: ""He's on the level, really down-to-earth. They were very cheery. The prince is a lovely man."" He added: ""I told him I was working with young kids and he said, 'Keep up all the good work.'"" Prince Charles also visited the Railway Hotel, at the invitation of his charity The Prince's Regeneration Trust. The trust hopes to restore and refurbish the building, where once Jimi Hendrix and The Clash played, as a new community and business centre." """) print(predictions) # 5. Save the model! trainer.save_checkpoint("summarization_model_xsum.pt")
parser.add_argument("--submission_path", type=str, required=True) parser.add_argument("--test_data_path", type=str, required=True) parser.add_argument("--best_model_path", type=str, required=True) # Optional parser.add_argument("--backbone", type=str, default="resnet18") parser.add_argument("--learning_rate", type=float, default=0.01) args = parser.parse_args() datamodule = ImageClassificationData.from_folders( train_folder=args.train_data_path, batch_size=8, ) model = ImageClassifier(datamodule.num_classes, backbone=args.backbone) trainer = Trainer(fast_dev_run=True) trainer.fit(model, datamodule=datamodule) trainer.save_checkpoint(args.best_model_path) datamodule = ImageClassificationData.from_folders( predict_folder=args.test_data_path, batch_size=8, ) predictions = Trainer().predict(model, datamodule=datamodule) submission_data = [{ "filename": os.path.basename(p["metadata"]["filepath"]), "label": torch.argmax(p["preds"]).item() } for batch in predictions for p in batch] df = pd.DataFrame(submission_data) df.to_csv(args.submission_path, index=False)